Skip to content

Commit e0b3f4a

Browse files
author
Rob Tjalma
authored
Merge pull request #93 from com-pas/fix-handling-404
Added check on NOT_FOUND to show correct message.
2 parents c04e0aa + f732ff4 commit e0b3f4a

File tree

4 files changed

+35
-5
lines changed

4 files changed

+35
-5
lines changed

src/compas-editors/CompasVersions.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {get, translate} from 'lit-translate';
33
import {newLogEvent, newWizardEvent, Wizard} from "../foundation.js";
44

55
import {CompasSclDataService, SDS_NAMESPACE} from "../compas-services/CompasSclDataService.js";
6-
import {createLogEvent} from "../compas-services/foundation.js";
6+
import {createLogEvent, isNotFoundError, NOT_FOUND_ERROR} from "../compas-services/foundation.js";
77
import {getTypeFromDocName, updateDocumentInOpenSCD} from "../compas/foundation.js";
88
import {getElementByName, getOpenScdElement, styles} from './foundation.js';
99
import {addVersionToCompasWizard} from "../compas/CompasUploadVersion.js";
@@ -53,6 +53,11 @@ export default class CompasVersionsPlugin extends LitElement {
5353
CompasSclDataService().listVersions(type, this.docId)
5454
.then(xmlResponse => {
5555
this.scls = Array.from(xmlResponse.querySelectorAll('Item') ?? []);
56+
})
57+
.catch(reason => {
58+
if (isNotFoundError(reason)) {
59+
this.scls = [];
60+
}
5661
});
5762
}
5863

src/compas-services/foundation.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ export function handleError(error: Error): Promise<never> {
6969
return Promise.reject({type: SERVER_ERROR, message: error.message});
7070
}
7171

72+
export function isNotFoundError(reason: any): boolean {
73+
return (reason.type !== undefined && reason.type === NOT_FOUND_ERROR);
74+
}
75+
7276
export function createLogEvent(reason: any): void {
7377
let message = reason.message;
7478
if (reason.status) {

src/compas/CompasExistsIn.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {property} from "lit-element";
33
import {LitElementConstructor, Mixin} from "../foundation.js";
44

55
import {CompasSclDataService} from "../compas-services/CompasSclDataService.js";
6-
import {NOT_FOUND_ERROR} from "../compas-services/foundation.js";
6+
import {isNotFoundError, NOT_FOUND_ERROR} from "../compas-services/foundation.js";
77
import {getTypeFromDocName} from "./foundation.js";
88

99
export type CompasExistsInElement = Mixin<typeof CompasExistsIn>;
@@ -33,7 +33,7 @@ export function CompasExistsIn<TBase extends LitElementConstructor>(Base: TBase)
3333
this.callService(docType, this.docId)
3434
.then(() => this.existInCompas = true)
3535
.catch(reason => {
36-
if (reason.type && reason.type === NOT_FOUND_ERROR) {
36+
if (isNotFoundError(reason)) {
3737
this.existInCompas = false;
3838
}
3939
});

test/unit/compas-services/foundation.test.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import {expect} from "@open-wc/testing";
22

3-
import {processErrorMessage} from "../../../src/compas-services/foundation.js";
3+
import {
4+
isNotFoundError,
5+
NOT_FOUND_ERROR,
6+
processErrorMessage,
7+
SERVER_ERROR
8+
} from "../../../src/compas-services/foundation.js";
49

510
const errorBody =
611
`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
@@ -19,13 +24,29 @@ describe('compas-services-foundation', () => {
1924
const result = await processErrorMessage(response);
2025
expect(result).to.be.equal(statusText);
2126
})
22-
2327
it('when there is a body in the response, the message is retrieved from the body', async () => {
2428
const expectedMessage = 'Name is not a correct name to be used later as filename. (CORE-8000)'
2529
const statusText = 'some status text';
2630
const response = new Response(errorBody, <ResponseInit>{statusText: statusText});
2731
const result = await processErrorMessage(response);
2832
expect(result).to.be.equal(expectedMessage);
33+
34+
})
35+
36+
it('when the error is caused by a status 404 then return true', () => {
37+
const reason = {type: NOT_FOUND_ERROR};
38+
const result = isNotFoundError(reason);
39+
expect(result).to.be.true;
40+
})
41+
it('when the error is caused by other status then return false', () => {
42+
const reason = {type: SERVER_ERROR};
43+
const result = isNotFoundError(reason);
44+
expect(result).to.be.false;
45+
})
46+
it('when no type of error found then return false', () => {
47+
const reason = {};
48+
const result = isNotFoundError(reason);
49+
expect(result).to.be.false;
2950
})
3051
});
3152

0 commit comments

Comments
 (0)