forked from camptocamp/ogc-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.ts
More file actions
156 lines (150 loc) · 4.3 KB
/
errors.ts
File metadata and controls
156 lines (150 loc) · 4.3 KB
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
import type { XmlDocument, XmlElement } from '@rgrove/parse-xml';
import {
findChildElement,
getElementAttribute,
getElementName,
getElementText,
getRootElement,
stripNamespace,
} from '../shared/xml-utils.js';
export class EndpointError extends Error {
constructor(
message: string,
public readonly httpStatus?: number,
public readonly isCrossOriginRelated?: boolean
) {
super(message);
this.name = 'EndpointError';
}
}
/**
* Representation of an Exception reported by an OWS service
*
* This is usually contained in a ServiceExceptionReport or ExceptionReport
* document and represented as a ServiceException or Exception element
*/
export class ServiceExceptionError extends Error {
/**
* Constructor
* @param message Error message
* @param requestUrl URL which resulted in the ServiceException
* @param code Optional ServiceException code
* @param locator Optional ServiceException locator
* @param response Optional response content received
*/
public constructor(
message: string,
public readonly requestUrl?: string,
public readonly code?: string,
public readonly locator?: string,
public readonly response?: XmlDocument
) {
super(message);
this.name = 'ServiceExceptionError';
}
}
/**
* Parse a ServiceException element to a ServiceExceptionError
* @param serviceException ServiceException element
* @param url URL from which the ServiceException was generated
*/
export function parse(
serviceException: XmlElement,
url?: string
): ServiceExceptionError {
const errorCode =
getElementAttribute(serviceException, 'code') ||
getElementAttribute(serviceException, 'exceptionCode');
const errorLocator = getElementAttribute(serviceException, 'locator');
const textElement =
findChildElement(serviceException, 'ExceptionText') || serviceException;
const errorMessage = getElementText(textElement).trim();
return new ServiceExceptionError(
errorMessage,
url,
errorCode,
errorLocator,
serviceException.document
);
}
/**
* Check the response for a ServiceExceptionReport and if present throw one
* @param response Response to check
* @param url URL from which response was generated
*/
export function check(response: XmlDocument, url?: string): XmlDocument {
const rootEl = getRootElement(response);
const rootElName = stripNamespace(getElementName(rootEl));
if (rootElName === 'ServiceExceptionReport') {
// document contains a ServiceExceptionReport, so generate an Error from
// the first ServiceException contained in it
const error = findChildElement(rootEl, 'ServiceException');
if (error) {
throw parse(error, url);
}
}
if (rootElName === 'ExceptionReport') {
const error = findChildElement(rootEl, 'Exception');
if (error) {
throw parse(error, url);
}
}
// there was nothing to convert to an Error so just pass the document on
return response;
}
/**
* This transforms an error object into a JSON-serializable object to be
* transferred from a worker
*/
export function encodeError(error: Error): Record<string, unknown> {
const base = {
message: error.message,
stack: error.stack,
name: error.name,
};
if (error instanceof ServiceExceptionError) {
return {
...base,
code: error.code,
locator: error.locator,
response: error.response,
requestUrl: error.requestUrl,
};
}
if (error instanceof EndpointError) {
return {
...base,
httpStatus: error.httpStatus,
isCrossOriginRelated: error.isCrossOriginRelated,
};
}
return base;
}
/**
* Recreates an error object
*/
export function decodeError(error: Record<string, unknown>): Error {
if (error.name === 'ServiceExceptionError') {
const e = new ServiceExceptionError(
error.message as string,
error.requestUrl as string,
error.code as string,
error.locator as string,
error.response as XmlDocument
);
e.stack = error.stack as string;
return e;
}
if (error.name === 'EndpointError') {
const e = new EndpointError(
error.message as string,
error.httpStatus as number,
error.isCrossOriginRelated as boolean
);
e.stack = error.stack as string;
return e;
}
const e = new Error(error.message as string);
e.stack = error.stack as string;
return e;
}