Add repository connections
Signed-off-by: trivernis <trivernis@protonmail.com>pull/4/head
parent
69c7ab7ac8
commit
68ffdc323b
@ -1,27 +1,50 @@
|
||||
use std::io::Error;
|
||||
use std::error::Error;
|
||||
use std::fmt::{Display, Formatter};
|
||||
use serde::Serialize;
|
||||
|
||||
pub type AppResult<T> = Result<T, AppError>;
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub enum AppError {
|
||||
Msg(String)
|
||||
pub struct AppError {
|
||||
message: String
|
||||
}
|
||||
|
||||
impl AppError {
|
||||
pub fn new<S: ToString>(msg: S) -> Self {
|
||||
Self {
|
||||
message: msg.to_string()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for AppError {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
self.message.fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
impl Error for AppError {}
|
||||
|
||||
impl From<std::io::Error> for AppError {
|
||||
fn from(e: Error) -> Self {
|
||||
Self::Msg(e.to_string())
|
||||
fn from(e: std::io::Error) -> Self {
|
||||
Self::new(e)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<toml::de::Error> for AppError {
|
||||
fn from(e: toml::de::Error) -> Self {
|
||||
Self::Msg(format!("Failed to deserialize toml: {:?}", e))
|
||||
Self::new(format!("Failed to deserialize toml: {:?}", e))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<toml::ser::Error> for AppError {
|
||||
fn from(e: toml::ser::Error) -> Self {
|
||||
Self::Msg(format!("Failed to serialize to toml: {:?}", e))
|
||||
Self::new(format!("Failed to serialize to toml: {:?}", e))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<rmp_ipc::error::Error> for AppError {
|
||||
fn from(e: rmp_ipc::error::Error) -> Self {
|
||||
Self::new(format!("Daemon Error: {:?}", e))
|
||||
}
|
||||
}
|
||||
|
@ -1,34 +1,42 @@
|
||||
import { Component } from '@angular/core';
|
||||
import {Component, OnInit} from '@angular/core';
|
||||
import {Router} from "@angular/router";
|
||||
import {RepositoryService} from "./services/repository/repository.service";
|
||||
import {DataloaderService} from "./services/dataloader/dataloader.service";
|
||||
import {MatSnackBar} from "@angular/material/snack-bar";
|
||||
import {ErrorBrokerService} from "./services/error-broker/error-broker.service";
|
||||
|
||||
@Component({
|
||||
selector: 'app-root',
|
||||
templateUrl: './app.component.html',
|
||||
styleUrls: ['./app.component.scss']
|
||||
})
|
||||
export class AppComponent {
|
||||
export class AppComponent implements OnInit{
|
||||
title = 'mediarepo-ui';
|
||||
|
||||
constructor(
|
||||
private router: Router,
|
||||
private snackBar: MatSnackBar,
|
||||
private errorBroker: ErrorBrokerService,
|
||||
private dataloaderService: DataloaderService,
|
||||
private repoService: RepositoryService
|
||||
private repoService: RepositoryService,
|
||||
) {
|
||||
}
|
||||
|
||||
async ngOnInit() {
|
||||
this.dataloaderService.loaderError.subscribe({
|
||||
error: (err) => {
|
||||
this.snackBar.open(err, undefined, {panelClass: "warn"})
|
||||
}
|
||||
})
|
||||
this.errorBroker.errorCb = (err: { message: string }) => {
|
||||
console.error(err);
|
||||
this.showError(err)
|
||||
};
|
||||
await this.dataloaderService.loadData();
|
||||
if (this.repoService.selectedRepository.getValue() == undefined) {
|
||||
await this.router.navigate(["repositories"])
|
||||
}
|
||||
}
|
||||
|
||||
private showError(err: { message: string }) {
|
||||
this.snackBar.open(err.message, undefined, {
|
||||
panelClass: "warn",
|
||||
duration: 2000,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -1,22 +1,20 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import {RepositoryService} from "../repository/repository.service";
|
||||
import {BehaviorSubject} from "rxjs";
|
||||
import {ErrorBrokerService} from "../error-broker/error-broker.service";
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class DataloaderService {
|
||||
|
||||
loaderError = new BehaviorSubject(undefined);
|
||||
|
||||
constructor(private repositoryService: RepositoryService) { }
|
||||
constructor(private erroBroker: ErrorBrokerService, private repositoryService: RepositoryService) { }
|
||||
|
||||
public async loadData() {
|
||||
try {
|
||||
await this.repositoryService.loadRepositories();
|
||||
} catch (err) {
|
||||
this.loaderError.error(err);
|
||||
console.error(err);
|
||||
this.erroBroker.showError(err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,16 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
|
||||
import { ErrorBrokerService } from './error-broker.service';
|
||||
|
||||
describe('ErrorBrokerService', () => {
|
||||
let service: ErrorBrokerService;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({});
|
||||
service = TestBed.inject(ErrorBrokerService);
|
||||
});
|
||||
|
||||
it('should be created', () => {
|
||||
expect(service).toBeTruthy();
|
||||
});
|
||||
});
|
@ -0,0 +1,22 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import {BehaviorSubject} from "rxjs";
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class ErrorBrokerService {
|
||||
|
||||
errorCb: Function | undefined;
|
||||
|
||||
constructor() { }
|
||||
|
||||
showError(error: {message: String}) {
|
||||
if (this.errorCb) {
|
||||
if (!error.message) {
|
||||
this.errorCb({message: error});
|
||||
} else {
|
||||
this.errorCb({...error});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue