Skip to content

Commit

Permalink
Merge pull request #77 from com-pas/rename-commons-ns
Browse files Browse the repository at this point in the history
Added better Error Handling
  • Loading branch information
Flurb authored Oct 5, 2021
2 parents d029d3f + 7b412d2 commit 6817056
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
37 changes: 33 additions & 4 deletions src/compas-services/foundation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,53 @@ export const NOT_FOUND_ERROR = 'NotFoundError';
export const APPLICATION_ERROR = 'ApplicationError';
export const SERVER_ERROR = 'ServerError';

export const COMMONS_NAMESPACE = 'https://www.lfenergy.org/compas/commons/v1';

export function getOpenScdElement(): OpenSCD {
return <OpenSCD>document.querySelector('open-scd');
}

export function handleResponse(response: Response): Promise<string> {
export async function handleResponse(response: Response): Promise<string> {
if (!response.ok) {
let type = APPLICATION_ERROR;
if (response.status === 404) {
type = NOT_FOUND_ERROR;
} else if (response.status >= 500)
{
} else if (response.status >= 500) {
type = SERVER_ERROR;
}
return Promise.reject({type: type, status: response.status, message: response.statusText});
return Promise.reject({type: type, status: response.status, message: await processErrorMessage(response)});
}
return Promise.resolve(response.text());
}

export async function processErrorMessage(response: Response): Promise<string> {
// default we will return the status text from the response.
let errorMessage = response.statusText;

const body = await response.text();
const doc = await parseXml(body);
const messages = Array.from(doc.querySelectorAll('ErrorResponse > ErrorMessage') ?? []);
// But if there are messages found in the body, we will process these and replace the status text with that.
if (messages.length > 0) {
errorMessage = '';
messages.forEach((errorMessageElement, index) => {
const code = errorMessageElement.getElementsByTagNameNS(COMMONS_NAMESPACE, "Code")!.item(0)!.textContent;
const message = errorMessageElement.getElementsByTagNameNS(COMMONS_NAMESPACE, "Message")!.item(0)!.textContent;

if (index > 0) {
errorMessage += ', ';
}

errorMessage += message;
if (code) {
errorMessage += ' (' + code + ')';
}
})
}

return errorMessage;
}

export function parseXml(textContent: string): Promise<Document> {
return Promise.resolve(new DOMParser().parseFromString(textContent, 'application/xml'));
}
Expand Down
31 changes: 31 additions & 0 deletions test/unit/compas-services/foundation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {expect} from "@open-wc/testing";

import {processErrorMessage} from "../../../src/compas-services/foundation.js";

const errorBody =
`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<compas-commons:ErrorResponse xmlns:compas-commons="https://www.lfenergy.org/compas/commons/v1">
<compas-commons:ErrorMessage>
<compas-commons:Code>CORE-8000</compas-commons:Code>
<compas-commons:Message>Name is not a correct name to be used later as filename.</compas-commons:Message>
<compas-commons:Property>create.request.name</compas-commons:Property>
</compas-commons:ErrorMessage>
</compas-commons:ErrorResponse>`;

describe('compas-services-foundation', () => {
it('when there is no body in the response, the status text will be returned', async () => {
const statusText = 'some status text';
const response = new Response(null, <ResponseInit>{statusText: statusText});
const result = await processErrorMessage(response);
expect(result).to.be.equal(statusText);
})

it('when there is a body in the response, the message is retrieved from the body', async () => {
const expectedMessage = 'Name is not a correct name to be used later as filename. (CORE-8000)'
const statusText = 'some status text';
const response = new Response(errorBody, <ResponseInit>{statusText: statusText});
const result = await processErrorMessage(response);
expect(result).to.be.equal(expectedMessage);
})
});

0 comments on commit 6817056

Please sign in to comment.