Skip to content

Commit

Permalink
Merge pull request #46 from terry-au/RO-20911-Add-spur-errors-types-t…
Browse files Browse the repository at this point in the history
…o-spur-errors-library

chore(types): [RO-20911] Add spur-errors types to spur-errors library
  • Loading branch information
acolchado authored Sep 20, 2023
2 parents d0c31b9 + 7d46a7b commit 3af2fdd
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ dist/
lib/
coverage/*
test/fixtures/**/*.js
src/SpurErrors.d.ts
41 changes: 41 additions & 0 deletions src/SpurErrors.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
export type SpurErrorCode = 400 | 401 | 403 | 404 | 405 | 408 | 409 | 500 | 502 | 503 | 504;

export interface BaseError<TErrorCode extends SpurErrorCode | undefined = undefined> {
create(message: string, internalError?: Error): this;
extend(statusCode: number, defaultMessage: string, errorCode: string): this;
set internalError(internalError: Error);
setErrorCode(errorCode: string): this;
setMessage(message: string): this;
setStatusCode(statusCode: number): this;
setData(data: unknown): this;

errorCode: TErrorCode;
}

interface SpurErrors {
BaseError: BaseError;

ValidationError: BaseError<400>;
UnauthorizedError: BaseError<401>;
ForbiddenError: BaseError<403>;
NotFoundError: BaseError<404>;
MethodNotAllowedError: BaseError<405>;
RequestTimeoutError: BaseError<408>;
AlreadyExistsError: BaseError<409>;
InternalServerError: BaseError<500>;
BadGatewayError: BaseError<502>;
ServiceUnavailableError: BaseError<503>;
GatewayTimeoutError: BaseError<504>;

errorByStatusCode: <TErrorCode extends SpurErrorCode>(statusCode: TErrorCode) => CodedError<TErrorCode>;
}

type CodedErrors = {
[P in keyof SpurErrors as SpurErrors[P] extends BaseError<infer TErrorCode> ? `${TErrorCode}` : never]: SpurErrors[P];
};

type CodedError<TErrorCode extends SpurErrorCode> = CodedErrors[`${TErrorCode}`];

declare const SpurErrors: SpurErrors;

export default SpurErrors;

0 comments on commit 3af2fdd

Please sign in to comment.