/
error.component.ts
125 lines (107 loc) · 3.81 KB
/
error.component.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import { Component, Inject, Input, OnInit } from '@angular/core';
import { Title } from '@angular/platform-browser';
import { ActivatedRoute } from '@angular/router';
import { ApiResponseData, ApiResponseError, HealthResponse, KnoraApiConnection } from '@dasch-swiss/dsp-js';
import { DspApiConnectionToken } from '../declarations/dsp-api-tokens';
export interface ErrorMsg {
status: number;
message: string;
description: string;
action: 'goback' | 'reload';
image: string;
}
@Component({
selector: 'app-error',
templateUrl: './error.component.html',
styleUrls: ['./error.component.scss']
})
export class ErrorComponent implements OnInit {
@Input() status: number;
@Input() comment?: string;
refresh = false;
// default error messages
errorMessages: ErrorMsg[] = [
{
status: 0,
message: 'Undefined Error',
description: `Maybe I'm a teapot, maybe not.<br>
But anyway, something undefined went wrong.`,
action: 'goback',
image: 'dsp-error.svg'
},
{
status: 403,
message: 'Forbidden',
description: `Invalid Permissions.<br>
Your request was valid but you do not have the<br>
necessary permissions to access it.`,
action: 'goback',
image: 'dsp-error-403.svg'
},
{
status: 404,
message: 'Not found',
description: 'The content you were looking for cannot be found.',
action: 'goback',
image: 'dsp-error-404.svg'
},
{
status: 500,
message: 'Internal Server Error',
description: `The DaSCH Service Platform is not available at the moment.<br>
An error has occured in a server side script.`,
action: 'reload',
image: 'dsp-error-500.svg'
},
{
status: 503,
message: 'Service unavailable',
description: `The DaSCH Service Platform is not available at the moment.<br>
The server is currently unavailable (overloaded or down).`,
action: 'reload',
image: 'dsp-error-503.svg'
}
];
// error message that will be shown in template
errorMessage: ErrorMsg;
constructor(
@Inject(DspApiConnectionToken) private _dspApiConnection: KnoraApiConnection,
private _titleService: Title,
private _route: ActivatedRoute
) { }
ngOnInit() {
// status is not defined in Input parameter
if (!this.status) {
// but status is defined in app.routing
this._route.data.subscribe(data => {
this.status = data.status;
});
}
// set the page title
this._titleService.setTitle('DSP | Error ' + this.status);
// get error message by status
this.errorMessage = this.getErrorMsgByStatus(this.status);
// if error message is not defined for the current status
// use the default error message
if (!this.errorMessage) {
this.errorMessage = this.errorMessages[0];
}
}
getErrorMsgByStatus(status: number): ErrorMsg {
return this.errorMessages.filter(x => x.status === status)[0];
}
reload() {
// get api health status first and reload page only, if the api is running and status is healthy
this.refresh = true;
this._dspApiConnection.system.healthEndpoint.getHealthStatus().subscribe(
(response: ApiResponseData<HealthResponse>) => {
if (response.body.status === 'healthy') {
window.location.reload();
}
},
(error: ApiResponseError) => {
this. refresh = false;
}
);
}
}