Коммит 672ebdc9 создал по автору keewek's avatar keewek
Просмотр файлов

feat(exception): add exception module

test(exception): add tests
владелец b52e8e9c
export class ErrorWithData extends Error {
private _data: any;
private _hasData: boolean = false;
readonly scope: string;
constructor(message: string | undefined, options?: { scope?: string; data?: any; }) {
super(message);
this.scope = options?.scope ?? '';
if (options && 'data' in options) {
this.data = options.data;
}
}
set data(data: any) {
this._hasData = true;
this._data = data;
}
get data(): any {
return this._data;
}
get hasData(): boolean {
return this._hasData;
}
scopedMessage(separator: string = ': '): string {
if (this.scope.length > 0) {
return this.scope + separator + this.message;
}
return this.message;
}
}
Object.defineProperty(ErrorWithData.prototype, 'name', {
value: 'ErrorWithData'
});
import * as assert from 'assert';
import { ErrorWithData } from '../../exception';
suite('Exception Test Suite', function () {
suite('ErrorWithData', function () {
test('name is "ErrorWithData"', function () {
const e = new ErrorWithData('msg');
assert.strictEqual(e.name, 'ErrorWithData');
});
test('scopedMessage without scope', function () {
const e = new ErrorWithData('msg');
assert.strictEqual(e.scopedMessage(), 'msg');
});
test('scopedMessage with scope', function () {
const e = new ErrorWithData('msg', {scope: 'scope'});
assert.strictEqual(e.scopedMessage(), 'scope: msg');
});
test('scopedMessage with scope and custom separator', function () {
const e = new ErrorWithData('msg', {scope: 'scope'});
assert.strictEqual(e.scopedMessage(' = '), 'scope = msg');
});
test('hasData is false with no data', function () {
const e = new ErrorWithData('msg', {scope: 'scope'});
assert.strictEqual(e.hasData, false);
});
test('data can be set to undefined', function () {
const e = new ErrorWithData('msg', {scope: 'scope', data: undefined});
assert.strictEqual(e.hasData, true);
assert.strictEqual(e.data, undefined);
});
});
});
Поддерживает Markdown
0% или .
You are about to add 0 people to the discussion. Proceed with caution.
Сначала завершите редактирование этого сообщения!
Пожалуйста, зарегистрируйтесь или чтобы прокомментировать