dnet-applications/frontends/dnet-is-application/src/app/is-utils.service.ts

36 lines
806 B
TypeScript
Raw Normal View History

2023-01-26 14:56:39 +01:00
import { Inject, Injectable } from '@angular/core';
2023-01-26 12:21:50 +01:00
import { FormGroup } from '@angular/forms';
2023-01-26 14:56:39 +01:00
import {MatSnackBar} from '@angular/material/snack-bar';
2023-01-26 12:21:50 +01:00
@Injectable({
providedIn: 'root'
})
2023-01-26 14:56:39 +01:00
export class ISUtilsService {
2023-01-26 12:21:50 +01:00
2023-01-26 14:56:39 +01:00
constructor(public snackBar: MatSnackBar) { }
2023-01-26 12:21:50 +01:00
prepareFormError(error:any, form:FormGroup): void {
2023-01-26 14:56:39 +01:00
form.setErrors({ serverError: this.errorMessage(error) })
}
snackError(error:any) {
this.snackBar.open(this.errorMessage(error), 'ERROR', {
duration: 5000,
});
}
alertError(error:any) {
alert(error);
}
private errorMessage(error:any) {
2023-01-26 12:21:50 +01:00
if (error.error && error.error.message) {
2023-01-26 14:56:39 +01:00
return error.error.message;
2023-01-26 12:21:50 +01:00
} else if (error.message) {
2023-01-26 14:56:39 +01:00
return error.message;
2023-01-26 12:21:50 +01:00
} else {
2023-01-26 14:56:39 +01:00
return 'Generic server side error';
2023-01-26 12:21:50 +01:00
}
}
}