This repository has been archived by the owner on Oct 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add errors and wrappers (and probably more idk)
- Loading branch information
Showing
9 changed files
with
334 additions
and
219 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,10 @@ | |
"comma-dangle": [ | ||
"warn", | ||
"always-multiline" | ||
], | ||
"new-parens": [ | ||
"warn", | ||
"never" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
import { Response } from "express"; | ||
|
||
export abstract class SharXError { | ||
code: string; | ||
data: unknown; | ||
httpCode: number; | ||
|
||
constructor(data: unknown, httpCode: number, code?: string) { | ||
this.code = code || this.constructor.name; | ||
this.data = data; | ||
this.httpCode = httpCode; | ||
} | ||
|
||
toJSON(): Record<string, unknown> { | ||
return { | ||
code: this.code, | ||
data: this.data, | ||
}; | ||
} | ||
|
||
send(res: Response): void { | ||
res.status(this.httpCode).json(this.toJSON()); | ||
} | ||
} | ||
|
||
export class UnknownError extends SharXError { | ||
constructor(data: unknown) { | ||
super(data, 500); | ||
} | ||
} | ||
|
||
export class MalformedRequestError extends SharXError { | ||
constructor(data: Record<string, unknown> = {}) { | ||
super(data, 400); | ||
} | ||
} | ||
|
||
interface JsonFieldErrorData extends Record<string, unknown> { | ||
field: string; | ||
} | ||
|
||
export class JsonFieldError extends MalformedRequestError { | ||
constructor(data: JsonFieldErrorData) { | ||
super(data); | ||
} | ||
} | ||
|
||
interface IllegalCharacterErrorData extends JsonFieldErrorData { | ||
character: string; | ||
} | ||
|
||
export class IllegalCharacterError extends JsonFieldError { | ||
constructor(data: IllegalCharacterErrorData) { | ||
super(data); | ||
} | ||
} | ||
|
||
interface TooShortFieldErrorData extends JsonFieldErrorData { | ||
minLength: number; | ||
} | ||
|
||
export class TooShortFieldError extends JsonFieldError { | ||
constructor(data: TooShortFieldErrorData) { | ||
super(data); | ||
} | ||
} | ||
|
||
interface TooLongFieldErrorData extends JsonFieldErrorData { | ||
maxLength: number; | ||
} | ||
|
||
export class TooLongFieldError extends JsonFieldError { | ||
constructor(data: TooLongFieldErrorData) { | ||
super(data); | ||
} | ||
} | ||
|
||
export class InvalidAuthHeaderError extends MalformedRequestError { | ||
constructor(data: Record<string, unknown> = {}) { | ||
super(data); | ||
} | ||
} | ||
|
||
export class InvalidCredentialsError extends SharXError { | ||
constructor(data: Record<string, unknown> = {}) { | ||
super(data, 401); | ||
} | ||
} | ||
|
||
export class InvalidTokenError extends InvalidCredentialsError { | ||
constructor(data: Record<string, unknown> = {}) { | ||
super(data); | ||
} | ||
} | ||
|
||
export class ExpiredTokenError extends InvalidTokenError { | ||
constructor(data: Record<string, unknown> = {}) { | ||
super(data); | ||
this.httpCode = 403; | ||
} | ||
} | ||
|
||
export class ResourceNotFoundError extends SharXError { | ||
constructor(data: Record<string, unknown> = {}) { | ||
super(data, 404); | ||
} | ||
} | ||
|
||
export class ImageNotFoundError extends ResourceNotFoundError { | ||
constructor(data: Record<string, unknown> = {}) { | ||
super(data); | ||
} | ||
} | ||
|
||
export class ResourceAlreadyExistsError extends SharXError { | ||
constructor(data: Record<string, unknown> = {}) { | ||
super(data, 400); | ||
} | ||
} | ||
|
||
interface FieldAlreadyExistsErrorData extends Record<string, unknown> { | ||
field: string; | ||
} | ||
|
||
export class FieldAlreadyExistsError extends ResourceAlreadyExistsError { | ||
constructor(data: FieldAlreadyExistsErrorData) { | ||
super(data); | ||
} | ||
} | ||
|
||
export class UserAlreadyRegisteredError extends ResourceAlreadyExistsError { | ||
constructor(data: FieldAlreadyExistsErrorData) { | ||
super(data); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.