/
status.component.ts
158 lines (137 loc) · 5.14 KB
/
status.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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 { HttpStatusMsg } from 'src/assets/http/statusMsg';
import { DspApiConnectionToken } from '../declarations/dsp-api-tokens';
export interface StatusMsg {
status: number;
message: string;
description: string;
action?: 'goback' | 'reload' | 'goto';
type?: 'info' | 'warning' | 'error';
image?: string;
}
@Component({
selector: 'app-status',
templateUrl: './status.component.html',
styleUrls: ['./status.component.scss']
})
export class StatusComponent implements OnInit {
@Input() status: number;
@Input() comment?: string;
@Input() url?: string;
@Input() representation?: 'archive' | 'audio' | 'document' | 'still-image' | 'video' | 'text';
refresh = false;
// error message that will be shown in template
message: StatusMsg;
// default error messages
errorMessages: StatusMsg[] = [
{
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: 204,
message: 'No Content',
description: `This content is not supported on small devices.
Please resize the browser window or switch to a desktop computer.`,
action: 'goback',
image: 'dsp-error.svg'
},
{
status: 403,
message: 'Forbidden',
description: `Invalid Permissions.<br>
Your request was valid but you do not have the
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.
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.
The server is currently unavailable (overloaded or down).`,
action: 'reload',
image: 'dsp-error-503.svg'
}
];
constructor(
@Inject(DspApiConnectionToken) private _dspApiConnection: KnoraApiConnection,
private _titleService: Title,
private _route: ActivatedRoute,
private _status: HttpStatusMsg
) { }
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;
this.comment = data.comment;
this.url = data.url;
});
}
// get error message by status
this.message = this.getMsgByStatus(this.status);
if (this.representation) {
this.comment = `There was an error loading the ${this.representation} file representation. Try to open it directly by clicking on the file url below:`;
this.message.action = 'goto';
} else {
// set the page title only in case of main error
this._titleService.setTitle(`DSP | ${this.getTypeByStatus(this.status).toUpperCase()} ${this.status}`);
}
}
getMsgByStatus(status: number): StatusMsg {
let msg = this.errorMessages.filter(x => x.status === status)[0];
if (!msg) {
msg = this._status.default[status];
msg.status = status;
msg.image = 'dsp-error.svg';
msg.action = (this.url ? 'goto' : undefined);
}
msg.type = this.getTypeByStatus(status);
return msg;
}
getTypeByStatus(status: number): 'info' | 'warning' | 'error' {
switch (true) {
case status >= 0 && status < 203: return 'info';
case status >= 203 && status < 400: return 'warning';
case status >= 400 && status < 600: return 'error';
}
}
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;
}
);
}
}