Skip to content
This repository was archived by the owner on May 1, 2020. It is now read-only.

Commit 404e538

Browse files
committed
test(errors): refactor error object and move toJson method to helpers
For whatever reason, this has not worked correctly for awhile
1 parent c9788ee commit 404e538

File tree

4 files changed

+51
-56
lines changed

4 files changed

+51
-56
lines changed

src/util/errors.spec.ts

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,27 @@ describe('Errors', () => {
55

66
describe('BuildError', () => {
77

8-
/*it('should create BuildError from err object in constructor', () => {
9-
const buildError = new BuildError();
10-
buildError.hasBeenLogged = false;
11-
buildError.message = 'message1';
8+
it('should create BuildError from err object in constructor', () => {
9+
const buildError = new BuildError('message1');
1210
buildError.name = 'name1';
1311
buildError.stack = 'stack1';
12+
buildError.isFatal = true;
13+
buildError.hasBeenLogged = true;
1414

1515
const buildErrorCopy = new BuildError(buildError);
16-
17-
const json = buildErrorCopy.toJson();
18-
expect(json.hasBeenLogged).toEqual(buildError.hasBeenLogged);
19-
expect(json.message).toEqual(buildError.message);
20-
expect(json.name).toEqual(buildError.name);
21-
expect(json.stack).toEqual(buildError.stack);
16+
expect(buildErrorCopy.message).toEqual(buildError.message);
17+
expect(buildErrorCopy.message).toEqual('message1');
18+
expect(buildErrorCopy.name).toEqual(buildError.name);
19+
expect(buildErrorCopy.stack).toEqual(buildError.stack);
20+
expect(buildErrorCopy.isFatal).toEqual(buildError.isFatal);
21+
expect(buildErrorCopy.hasBeenLogged).toEqual(buildError.hasBeenLogged);
2222
});
2323

24-
it('should create json object', () => {
25-
const buildError = new BuildError();
26-
buildError.hasBeenLogged = false;
27-
buildError.message = 'message';
28-
buildError.name = 'name';
29-
buildError.stack = 'stack';
30-
const json = buildError.toJson();
31-
expect(json.hasBeenLogged).toEqual(buildError.hasBeenLogged);
32-
expect(json.message).toEqual(buildError.message);
33-
expect(json.name).toEqual(buildError.name);
34-
expect(json.stack).toEqual(buildError.stack);
35-
});*/
36-
37-
it('should stop npm run test from cancelling', () => {
38-
24+
it('should create a default object', () => {
25+
const buildError = new BuildError('message1');
26+
expect(buildError.isFatal).toBeFalsy();
27+
expect(buildError.hasBeenLogged).toBeFalsy();
3928
});
40-
4129
});
4230

4331
});

src/util/errors.ts

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,16 @@ export class BuildError extends Error {
33
hasBeenLogged = false;
44
isFatal: boolean = false;
55

6-
constructor(err?: any) {
7-
super();
8-
9-
if (err) {
10-
if (err.message) {
11-
this.message = err.message;
12-
} else if (err) {
13-
this.message = err;
14-
}
15-
if (err.stack) {
16-
this.stack = err.stack;
17-
}
18-
if (err.name) {
19-
this.name = err.name;
20-
}
21-
if (typeof err.hasBeenLogged === 'boolean') {
22-
this.hasBeenLogged = err.hasBeenLogged;
23-
}
24-
if (err.hasOwnProperty('isFatal')) {
25-
this.isFatal = err.isFatal;
26-
}
6+
constructor(error: Error | string) {
7+
super(error instanceof Error ? error.message : error);
8+
if (error instanceof Error) {
9+
this.message = error.message;
10+
this.stack = error.stack;
11+
this.name = error.name;
12+
this.hasBeenLogged = (error as BuildError).hasBeenLogged;
13+
this.isFatal = (error as BuildError).isFatal;
2714
}
2815
}
29-
30-
toJson() {
31-
return {
32-
message: this.message,
33-
name: this.name,
34-
stack: this.stack,
35-
hasBeenLogged: this.hasBeenLogged
36-
};
37-
}
3816
}
3917

4018

src/util/helpers.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { BuildError } from './errors';
12
import * as helpers from './helpers';
23

34
let originalEnv: any = null;
@@ -190,4 +191,21 @@ describe('helpers', () => {
190191
expect(helpers.replaceAll('hello $VAR world', '$VAR', undefined)).toEqual('hello world');
191192
});
192193
});
194+
195+
describe('buildErrorToJson', () => {
196+
it('should return a pojo', () => {
197+
const buildError = new BuildError('message1');
198+
buildError.name = 'name1';
199+
buildError.stack = 'stack1';
200+
buildError.isFatal = true;
201+
buildError.hasBeenLogged = false;
202+
203+
const object = helpers.buildErrorToJson(buildError);
204+
expect(object.message).toEqual('message1');
205+
expect(object.name).toEqual(buildError.name);
206+
expect(object.stack).toEqual(buildError.stack);
207+
expect(object.isFatal).toEqual(buildError.isFatal);
208+
expect(object.hasBeenLogged).toEqual(buildError.hasBeenLogged);
209+
});
210+
});
193211
});

src/util/helpers.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { createReadStream, createWriteStream, ensureDir, readdir, readFile, read
44
import * as osName from 'os-name';
55

66
import * as Constants from './constants';
7+
import { BuildError } from './errors';
78
import { BuildContext, DeepLinkConfigEntry, File, WebpackStats } from './interfaces';
89
import { Logger } from '../logger/logger';
910

@@ -365,3 +366,13 @@ export function removeSuffix(input: string, suffix: string) {
365366

366367
return input;
367368
}
369+
370+
export function buildErrorToJson(buildError: BuildError) {
371+
return {
372+
message: buildError.message,
373+
name: buildError.name,
374+
stack: buildError.stack,
375+
hasBeenLogged: buildError.hasBeenLogged,
376+
isFatal: buildError.isFatal
377+
};
378+
}

0 commit comments

Comments
 (0)