Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use default options when none provided #29

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ExtendableError from 'extendable-error';
export interface ErrorConfig {
message: string;
message?: string;
time_thrown?: string;
data?: object;
options?: {
Expand All @@ -25,7 +25,7 @@ export declare class ApolloError extends ExtendableError {
locations: any;
_showLocations: boolean;
_showPath: boolean;
constructor(name: string, config: ErrorConfig);
constructor(name: string, baseConfig?: ErrorConfig, config?: ErrorConfig);
serialize(): ErrorInfo;
}
export declare const isInstance: (e: any) => boolean;
Expand Down
22 changes: 10 additions & 12 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 10 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const isString = d => Object.prototype.toString.call(d) === '[object String]';
const isObject = d => Object.prototype.toString.call(d) === '[object Object]';

export interface ErrorConfig {
message: string;
message?: string;
time_thrown?: string;
data?: object;
options?: {
Expand Down Expand Up @@ -33,22 +33,17 @@ export class ApolloError extends ExtendableError {
_showLocations: boolean = false;
_showPath: boolean = false;

constructor(name: string, config: ErrorConfig) {
super((arguments[2] && arguments[2].message) || '');

const t = (arguments[2] && arguments[2].time_thrown) || (new Date()).toISOString();
const m = (arguments[2] && arguments[2].message) || '';
const configData = (arguments[2] && arguments[2].data) || {};
const d = { ...this.data, ...configData };
const opts = ((arguments[2] && arguments[2].options) || {});

constructor(name: string, baseConfig: ErrorConfig = {}, config: ErrorConfig = {}) {
super(config.message || baseConfig.message || '');

const options = {...config.options, ...baseConfig.options};

this.name = name;
this.message = m;
this.time_thrown = t;
this.data = d;
this._showLocations = !!opts.showLocations;
this._showPath = !!opts.showPath;
this.message = config.message || baseConfig.message || '';
this.time_thrown = config.time_thrown || baseConfig.time_thrown || new Date().toISOString();
this.data = {...baseConfig.data, ...config.data};
this._showLocations = !!options.showLocations;
this._showPath = !!options.showPath;
}

serialize(): ErrorInfo {
Expand Down
20 changes: 20 additions & 0 deletions test/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,26 @@ describe('createError', () => {
foo: 'bar'
});
});

it('uses original message and merges original data', () => {
const FooError = createError('FooError', {
message: 'A foo error has occurred',
data: {
hello: 'world'
},
options: {
showLocations: false,
showPath: true
}
});
const e = new FooError({data: {foo: 'bar'}});

const { message, name, time_thrown, data } = e.serialize();
expect(message).to.equal('A foo error has occurred');
expect(name).to.equal('FooError');
expect(time_thrown).to.equal(e.time_thrown);
expect(data).to.eql({ hello: 'world', foo: 'bar' });
});
});
context('when missing a config as the second parameter', () => {
it('throws an assertion error with a useful message', () => {
Expand Down