Skip to content

Commit 6817056

Browse files
author
Rob Tjalma
authored
Merge pull request #77 from com-pas/rename-commons-ns
Added better Error Handling
2 parents d029d3f + 7b412d2 commit 6817056

File tree

2 files changed

+64
-4
lines changed

2 files changed

+64
-4
lines changed

src/compas-services/foundation.ts

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,53 @@ export const NOT_FOUND_ERROR = 'NotFoundError';
77
export const APPLICATION_ERROR = 'ApplicationError';
88
export const SERVER_ERROR = 'ServerError';
99

10+
export const COMMONS_NAMESPACE = 'https://www.lfenergy.org/compas/commons/v1';
11+
1012
export function getOpenScdElement(): OpenSCD {
1113
return <OpenSCD>document.querySelector('open-scd');
1214
}
1315

14-
export function handleResponse(response: Response): Promise<string> {
16+
export async function handleResponse(response: Response): Promise<string> {
1517
if (!response.ok) {
1618
let type = APPLICATION_ERROR;
1719
if (response.status === 404) {
1820
type = NOT_FOUND_ERROR;
19-
} else if (response.status >= 500)
20-
{
21+
} else if (response.status >= 500) {
2122
type = SERVER_ERROR;
2223
}
23-
return Promise.reject({type: type, status: response.status, message: response.statusText});
24+
return Promise.reject({type: type, status: response.status, message: await processErrorMessage(response)});
2425
}
2526
return Promise.resolve(response.text());
2627
}
2728

29+
export async function processErrorMessage(response: Response): Promise<string> {
30+
// default we will return the status text from the response.
31+
let errorMessage = response.statusText;
32+
33+
const body = await response.text();
34+
const doc = await parseXml(body);
35+
const messages = Array.from(doc.querySelectorAll('ErrorResponse > ErrorMessage') ?? []);
36+
// But if there are messages found in the body, we will process these and replace the status text with that.
37+
if (messages.length > 0) {
38+
errorMessage = '';
39+
messages.forEach((errorMessageElement, index) => {
40+
const code = errorMessageElement.getElementsByTagNameNS(COMMONS_NAMESPACE, "Code")!.item(0)!.textContent;
41+
const message = errorMessageElement.getElementsByTagNameNS(COMMONS_NAMESPACE, "Message")!.item(0)!.textContent;
42+
43+
if (index > 0) {
44+
errorMessage += ', ';
45+
}
46+
47+
errorMessage += message;
48+
if (code) {
49+
errorMessage += ' (' + code + ')';
50+
}
51+
})
52+
}
53+
54+
return errorMessage;
55+
}
56+
2857
export function parseXml(textContent: string): Promise<Document> {
2958
return Promise.resolve(new DOMParser().parseFromString(textContent, 'application/xml'));
3059
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import {expect} from "@open-wc/testing";
2+
3+
import {processErrorMessage} from "../../../src/compas-services/foundation.js";
4+
5+
const errorBody =
6+
`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
7+
<compas-commons:ErrorResponse xmlns:compas-commons="https://www.lfenergy.org/compas/commons/v1">
8+
<compas-commons:ErrorMessage>
9+
<compas-commons:Code>CORE-8000</compas-commons:Code>
10+
<compas-commons:Message>Name is not a correct name to be used later as filename.</compas-commons:Message>
11+
<compas-commons:Property>create.request.name</compas-commons:Property>
12+
</compas-commons:ErrorMessage>
13+
</compas-commons:ErrorResponse>`;
14+
15+
describe('compas-services-foundation', () => {
16+
it('when there is no body in the response, the status text will be returned', async () => {
17+
const statusText = 'some status text';
18+
const response = new Response(null, <ResponseInit>{statusText: statusText});
19+
const result = await processErrorMessage(response);
20+
expect(result).to.be.equal(statusText);
21+
})
22+
23+
it('when there is a body in the response, the message is retrieved from the body', async () => {
24+
const expectedMessage = 'Name is not a correct name to be used later as filename. (CORE-8000)'
25+
const statusText = 'some status text';
26+
const response = new Response(errorBody, <ResponseInit>{statusText: statusText});
27+
const result = await processErrorMessage(response);
28+
expect(result).to.be.equal(expectedMessage);
29+
})
30+
});
31+

0 commit comments

Comments
 (0)