Skip to content

Commit

Permalink
Create BadgehubApiError base
Browse files Browse the repository at this point in the history
For improved API error handling. If we create many more after this we
can add more types based on the base error.
  • Loading branch information
Pauline Vos committed Nov 13, 2024
1 parent 59b4a59 commit 44cc7c0
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
enum ErrorType {
NotAuthorized = "ERROR_NOT_AUTHORIZED",
NotAuthenticated = "ERROR_NOT_AUTHENTICATED",
}
class BadgeHubApiError extends Error {
name: ErrorType;
message: string;
cause: any;

constructor({
name,
message,
cause,
}: {
name: ErrorType;
message: string;
cause?: any;
}) {
super();
this.name = name;
this.message = message;
this.cause = cause;
}
}
const NotAuthenticatedError = (
message: string = "You are not authenticated"
): BadgeHubApiError => {
return new BadgeHubApiError({ name: ErrorType.NotAuthenticated, message });
};

const NotAuthorizedError = (
message: string = "You are not authorized"
): BadgeHubApiError => {
return new BadgeHubApiError({ name: ErrorType.NotAuthorized, message });
};

export {
ErrorType,
BadgeHubApiError,
NotAuthorizedError,
NotAuthenticatedError,
};

0 comments on commit 44cc7c0

Please sign in to comment.