1
- import { get } from "lit-translate" ;
2
- import { newLogEvent } from "../foundation.js" ;
3
- import { dispatchEventOnOpenScd } from "../compas/foundation.js" ;
1
+ import { get } from 'lit-translate' ;
2
+ import { newLogEvent } from '../foundation.js' ;
4
3
5
4
export const NOT_FOUND_ERROR = 'NotFoundError' ;
6
5
export const APPLICATION_ERROR = 'ApplicationError' ;
@@ -16,7 +15,11 @@ export async function handleResponse(response: Response): Promise<string> {
16
15
} else if ( response . status >= 500 ) {
17
16
type = SERVER_ERROR ;
18
17
}
19
- return Promise . reject ( { type : type , status : response . status , message : await processErrorMessage ( response ) } ) ;
18
+ return Promise . reject ( {
19
+ type : type ,
20
+ status : response . status ,
21
+ message : await processErrorMessage ( response ) ,
22
+ } ) ;
20
23
}
21
24
return Promise . resolve ( response . text ( ) ) ;
22
25
}
@@ -27,13 +30,19 @@ export async function processErrorMessage(response: Response): Promise<string> {
27
30
28
31
const body = await response . text ( ) ;
29
32
const doc = await parseXml ( body ) ;
30
- const messages = Array . from ( doc . querySelectorAll ( 'ErrorResponse > ErrorMessage' ) ?? [ ] ) ;
33
+ const messages = Array . from (
34
+ doc . querySelectorAll ( 'ErrorResponse > ErrorMessage' ) ?? [ ]
35
+ ) ;
31
36
// But if there are messages found in the body, we will process these and replace the status text with that.
32
37
if ( messages . length > 0 ) {
33
38
errorMessage = '' ;
34
39
messages . forEach ( ( errorMessageElement , index ) => {
35
- const code = errorMessageElement . getElementsByTagNameNS ( COMMONS_NAMESPACE , "Code" ) ! . item ( 0 ) ! . textContent ;
36
- const message = errorMessageElement . getElementsByTagNameNS ( COMMONS_NAMESPACE , "Message" ) ! . item ( 0 ) ! . textContent ;
40
+ const code = errorMessageElement
41
+ . getElementsByTagNameNS ( COMMONS_NAMESPACE , 'Code' ) !
42
+ . item ( 0 ) ! . textContent ;
43
+ const message = errorMessageElement
44
+ . getElementsByTagNameNS ( COMMONS_NAMESPACE , 'Message' ) !
45
+ . item ( 0 ) ! . textContent ;
37
46
38
47
if ( index > 0 ) {
39
48
errorMessage += ', ' ;
@@ -43,55 +52,70 @@ export async function processErrorMessage(response: Response): Promise<string> {
43
52
if ( code ) {
44
53
errorMessage += ' (' + code + ')' ;
45
54
}
46
- } )
55
+ } ) ;
47
56
}
48
57
49
58
return errorMessage ;
50
59
}
51
60
52
61
export function parseXml ( textContent : string ) : Promise < Document > {
53
- return Promise . resolve ( new DOMParser ( ) . parseFromString ( textContent , 'application/xml' ) ) ;
62
+ return Promise . resolve (
63
+ new DOMParser ( ) . parseFromString ( textContent , 'application/xml' )
64
+ ) ;
54
65
}
55
66
56
67
export function extractSclFromResponse ( response : Document ) : Promise < Document > {
57
68
// Extract the SCL Result from the Response and create a new Document from it.
58
- const sclData = response . querySelectorAll ( "SclData" ) . item ( 0 ) . textContent ?? '' ;
59
- const sclDocument = new DOMParser ( ) . parseFromString ( sclData , 'application/xml' ) ;
69
+ const sclData =
70
+ response . querySelectorAll ( 'SclData' ) . item ( 0 ) . textContent ?? '' ;
71
+ const sclDocument = new DOMParser ( ) . parseFromString (
72
+ sclData ,
73
+ 'application/xml'
74
+ ) ;
60
75
return Promise . resolve ( sclDocument ) ;
61
76
}
62
77
63
78
export function handleError ( error : Error ) : Promise < never > {
64
- return Promise . reject ( { type : SERVER_ERROR , message : error . message } ) ;
79
+ return Promise . reject ( { type : SERVER_ERROR , message : error . message } ) ;
65
80
}
66
81
67
- export function createLogEvent ( reason : any ) : void {
82
+ export function createLogEvent ( element : Element , reason : any ) : void {
68
83
let message = reason . message ;
69
84
if ( reason . status ) {
70
- message += " (" + reason . status + ")" ;
85
+ message += ' (' + reason . status + ')' ;
71
86
}
72
87
73
- dispatchEventOnOpenScd (
88
+ element . dispatchEvent (
74
89
newLogEvent ( {
75
90
kind : 'error' ,
76
91
title : get ( 'compas.error.server' ) ,
77
- message : get ( 'compas.error.serverDetails' , { type : reason . type , message : message } )
78
- } ) ) ;
92
+ message : get ( 'compas.error.serverDetails' , {
93
+ type : reason . type ,
94
+ message : message ,
95
+ } ) ,
96
+ } )
97
+ ) ;
79
98
}
80
99
81
100
export function getWebsocketUri ( settingsUrl : string ) : string {
82
- if ( settingsUrl . startsWith ( "http://" ) || settingsUrl . startsWith ( "https://" ) ) {
83
- return settingsUrl . replace ( "http://" , "ws://" ) . replace ( "https://" , "wss://" ) ;
101
+ if ( settingsUrl . startsWith ( 'http://' ) || settingsUrl . startsWith ( 'https://' ) ) {
102
+ return settingsUrl
103
+ . replace ( 'http://' , 'ws://' )
104
+ . replace ( 'https://' , 'wss://' ) ;
84
105
}
85
106
86
- return ( document . location . protocol == "http:" ? "ws://" : "wss://" )
87
- + document . location . hostname + ":" + getWebsocketPort ( )
88
- + settingsUrl ;
107
+ return (
108
+ ( document . location . protocol == 'http:' ? 'ws://' : 'wss://' ) +
109
+ document . location . hostname +
110
+ ':' +
111
+ getWebsocketPort ( ) +
112
+ settingsUrl
113
+ ) ;
89
114
}
90
115
91
116
export function getWebsocketPort ( ) : string {
92
- if ( document . location . port === "" ) {
93
- return ( document . location . protocol == " http:" ? "80" : " 443" )
117
+ if ( document . location . port === '' ) {
118
+ return document . location . protocol == ' http:' ? '80' : ' 443' ;
94
119
}
95
120
return document . location . port ;
96
121
}
97
-
0 commit comments