Skip to content

Commit

Permalink
feat(errors): update how errors are created
Browse files Browse the repository at this point in the history
  • Loading branch information
pxseu committed Nov 1, 2021
1 parent 231b5e1 commit 4dd8117
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/lib/errors/HTTPErrors/Aborted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ export class Aborted extends ImperialError {
constructor(...args: ConstructorParameters<typeof ImperialError>) {
super(...args);

this.message = "Requst was aborted.";
this.message = "Requst was aborted";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export class NotAllowed extends ImperialError {
constructor(...args: ConstructorParameters<typeof ImperialError>) {
super(...args);

this.message = "Sorry! You aren't allowed to modify this document.";
this.message = "You're not authorized for this action";
this.status = 401;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ImperialError } from "../ImperialError";

export class DocumentNotFound extends ImperialError {
export class NotFound extends ImperialError {
constructor(...args: ConstructorParameters<typeof ImperialError>) {
super(...args);

this.message = "We could not find that Document!";
this.message = "We could not find that Document or User";
this.status = 404;
}
}
10 changes: 0 additions & 10 deletions src/lib/errors/Imperial/WrongPassword.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/lib/errors/ImperialError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export class ImperialError extends Error {

constructor(errorData: ImperialErrorInterface = {}) {
super(errorData?.message);

this.name = "ImperialError";

if ("status" in errorData) this.status = errorData.status;
Expand Down
39 changes: 21 additions & 18 deletions src/lib/errors/Messages.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
export const NO_TEXT = "No `text` was provided!";

export const TEXT_WRONG_TYPE = "Parameter `text` must be a string!";

export const NO_TOKEN = "This method requires a token to be set in the constructor!";

export const NO_ID = "No `id` was provided!";

export const ID_WRONG_TYPE = "Parameter `id` must be a string or an URL!";

export const PASSWORD_WRONG_TYPE = "Parameter `password` must be a string!";

export const OPTIONS_WRONG_TYPE = "Parameter `options` must be an Object!";

export const SCHEMA_FAILED_VALIDATION = (key: string, message: string, required: boolean = false): string =>
`Property "${key}" ${required ? "is required and " : ""}must be ${message}`;

export const SCHEMA_INVALID_KEY = (key: string): string => `They key ${key} is not on the schema`;
const Messages = {
NO_TEXT: "No `text` was provided",
TEXT_WRONG_TYPE: "Parameter `text` must be a string",
NO_TOKEN: "This method requires a token to be set in the constructor",
NO_ID: "No `id` was provided",
NO_USERNAME: "No `username` was provided",
ID_WRONG_TYPE: "Parameter `id` must be a string or an URL",
USERNAME_WRONG_TYPE: "Parameter `username` must be a string",
PASSWORD_WRONG_TYPE: "Parameter `password` must be a string",
OPTIONS_WRONG_TYPE: "Parameter `options` must be an Object",
SCHEMA_FAILED_VALIDATION(key: string, message: string, required: boolean = false): string {
return `Property "${key}" ${required ? "is required and " : ""}must be ${message}`;
},
SCHEMA_INVALID_KEY(key: string): string {
return `They key "${key}" is not on the schema`;
},
} as const;

type MessageKeys = keyof typeof Messages;

export const ErrorMessage = <T extends MessageKeys>(key: T) => Messages[key] as typeof Messages[T];
5 changes: 2 additions & 3 deletions src/lib/errors/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export { DocumentNotFound } from "./Imperial/DocumentNotFound";
export { NotFound } from "./Imperial/NotFound";
export { ImperialError } from "./ImperialError";
export { Aborted } from "./HTTPErrors/Aborted";
export { FailedToFetch } from "./HTTPErrors/FailedToFetch";
export { NotAllowed } from "./Imperial/EditNotAllowed";
export { WrongPassword } from "./Imperial/WrongPassword";
export { NotAllowed } from "./Imperial/NotAllowed";

0 comments on commit 4dd8117

Please sign in to comment.