8000 Closes #10 · gucong3000/plugin-error@bd5651c · GitHub
[go: up one dir, main page]

Skip to content

Commit bd5651c

Browse files
committed
Closes gulpjs#10
1 parent 4dfcef3 commit bd5651c

File tree

2 files changed

+53
-9
lines changed

2 files changed

+53
-9
lines changed

index.d.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,10 @@ declare namespace PluginError {
77

88
/**
99
* @param plugin Plugin name
10-
* @param message Error message
10+
* @param error Base error / Error message
1111
* @param options Error options
1212
*/
13-
new (plugin: string, message: string, options?: Options): PluginError;
14-
15-
/**
16-
* @param plugin Plugin name
17-
* @param error Base error
18-
* @param options Error options
19-
*/
20-
new <E extends Error>(plugin: string, error: E, options?: Options): PluginError<E>;
13+
new <E extends Error>(plugin: string, error: E | string, options?: Options): PluginError<E>;
2114

2215
/**
2316
* @param plugin Plugin name

test/types/test.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,54 @@ import PluginError = require("plugin-error");
5151
}
5252
}
5353

54+
describe('PluginError()', function() {
55+
it('support error type define with "Error | string"', (done) => {
56+
const PLUGIN_NAME = 'test';
57+
function createPluginError(err: Error | string) {
58+
return new PluginError(PLUGIN_NAME, err);
59+
}
60+
expect(createPluginError('something broke').message).to.equal('something broke');
61+
expect(createPluginError(new Error('something broke')).message).to.equal('something broke');
62+
done();
63+
});
64+
65+
it('Inference with union type on second parameter', (done) => {
66+
const PLUGIN_NAME = 'test';
67+
68+
interface IFooError extends Error {
69+
foo: number;
70+
}
71+
72+
function createPluginError(err: IFooError | string) {
73+
return new PluginError(PLUGIN_NAME, err);
74+
}
75+
76+
const fooError: IFooError = Object.assign(new Error('something broke'), {foo: 1});
77+
const pluginError = createPluginError(fooError);
78+
const foo: number = pluginError.foo;
79+
expect(foo).to.be(1);
80+
done();
81+
});
82+
83+
it('Inference with union type on second parameter and dependent properties', (done) => {
84+
// Inference with union type on second parameter and dependent properties
85+
const PLUGIN_NAME = 'test';
86+
87+
interface IFooBarError extends Error {
88+
foo: number;
89+
bar: number;
90+
}
91+
92+
function createPluginError(err: IFooBarError | string) {
93+
return new PluginError(PLUGIN_NAME, err);
94+
}
95+
96+
const fooError: IFooBarError = Object.assign(new Error('something broke'), {foo: 1, bar: 2});
97+
const pluginError = createPluginError(fooError);
98+
const foo: number = pluginError.foo;
99+
const bar: number = pluginError.bar;
100+
expect(foo).to.be(1);
101+
expect(bar).to.be(2);
102+
done();
103+
});
104+
});

0 commit comments

Comments
 (0)
0