/
error-handler.ts
46 lines (42 loc) · 1.38 KB
/
error-handler.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { throwError } from 'rxjs';
import { AppError, HttpAppError } from './error.model';
import { LoggerService } from '../services/logger.service';
import { HttpErrorResponse } from '@angular/common/http';
export class ErrorHandler {
/**
* Generate the error message that will be used and throw the appropriate exception.
* @param error the exception that was thrown by the http post.
* @returns {any} Exception to be thrown by the Observable
*/
public handleError(error: Response | any) {
const errorObject = {
status: 0,
message: ''
};
if (error instanceof HttpErrorResponse) {
let body;
errorObject.status = error.status;
try {
body = error.error || '';
} catch (e) {
LoggerService.log('Unparsable json', error);
errorObject.message = `${error} (Status code: ${error.status})`;
}
if (body) {
let isFirst = true;
for (const bodyElement of body) {
if (!isFirst) {
errorObject.message += '\n';
} else {
isFirst = false;
}
errorObject.message += bodyElement.message;
}
}
return throwError(new HttpAppError(errorObject.message, errorObject.status));
} else {
errorObject.message = error.message ? error.message : error.toString();
return throwError(new AppError(errorObject.message));
}
}
}