Skip to content

Commit

Permalink
feat(core): removes generateErrors; adds CollectionError
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed Oct 13, 2019
1 parent 625c0d0 commit 221e85d
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 73 deletions.
26 changes: 0 additions & 26 deletions packages/core/src/PublicError.ts

This file was deleted.

44 changes: 44 additions & 0 deletions packages/core/src/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { ErrorCode, CollectionTree } from '~/types';
import { isTypeError } from './utils';

export class PublicError extends Error {
public id: string;
public code: ErrorCode;
public source?: Error;
public constructor(
id: string,
code: ErrorCode,
source?: Error | null,
message?: string
) {
super(message);
this.id = id;
this.code = code;
this.source = source || undefined;
}
public get name(): string {
return 'PublicError';
}
public get root(): PublicError {
return this.source instanceof PublicError && this.source !== this
? this.source.root
: this;
}
}

export class CollectionError<
T extends CollectionTree,
K extends keyof T['types']
> extends PublicError {
public constructor(collection: T, id: K, source?: Error | null) {
if (!Object.hasOwnProperty.call(collection.types, id)) {
throw Error(`Type "${id}" does not exist on collection`);
}

const error = (collection.types as any)[id];
if (!isTypeError(error)) {
throw Error(`Type "${id}" is not an error on collection`);
}
super(id as string, error.code, source, error.description);
}
}
43 changes: 0 additions & 43 deletions packages/core/src/generate/errors.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/core/src/generate/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './typings';
export * from './errors';
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ export * from './create';
export * from './generate';
export * from './utils';
export * from './types';
export { default as PublicError } from './PublicError';
export * from './errors';
4 changes: 2 additions & 2 deletions packages/core/src/types/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ export type ErrorCode = ClientErrorCode | ServerErrorCode;

export type ClientErrorCode =
| 'ClientError'
| 'ClientNotFound'
| 'ClientForbidden'
| 'ClientUnauthorized'
| 'ClientForbidden'
| 'ClientNotFound'
| 'ClientConflict'
| 'ClientUnsupported'
| 'ClientTooEarly'
Expand Down

0 comments on commit 221e85d

Please sign in to comment.