Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ 👷‍♂️ Error Handler #504

Merged
merged 9 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import { mapGetters } from "vuex";
import { defineAsyncComponent } from "vue";
import axios from "axios";

export default {
data() {
return {
Expand Down
71 changes: 71 additions & 0 deletions src/helpers/errorHandler/ErrorHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { errorRegistry } from "./errorRegistry";
import store from "@/store";

class ErrorHandler {
static handleError(error: Error) {
const errorType = this.identifyErrorType(error);

//@ts-ignore
const errorData = errorRegistry[errorType] || errorRegistry.GENERIC_ERROR;

const message = this.getErrorMessage(
errorData.template,
//@ts-ignore
error.shortMessage ? error.shortMessage : error.message
);

// Show error notification
this.setErrorNotifictiom(message);

console.log(`Error:`, { errorType, errorData, message });
}

static identifyErrorType(error: Error) {
const checks = [this.identifyCustomThrownError, this.identifyViemError];

for (const check of checks) {
const errorType = check(error);
if (errorType) return errorType;
}

return "GENERIC_ERROR";
}

static identifyCustomThrownError(error: Error) {
if (error.message?.includes("CUSTOM")) return error.message;
return null;
}

static identifyViemError(error: any) {
if (error.version?.includes("viem")) return "VIEM_ERROR";
return null;
}

static identifyAxiosError(error: any) {
if (error.isAxios) return "AXIOS_ERROR";
return null;
}

static getErrorMessage(template: string, messageFromError: string) {
let finalMessage = template;
if (template.includes("{message}")) {
finalMessage = finalMessage.replace(
"{message}",
messageFromError || "Details not provided."
);
}

return finalMessage;
}

static setErrorNotifictiom(message: string) {
store.commit("notifications/deleteAll");

store.dispatch("notifications/new", {
msg: message,
type: "error",
});
}
}

export default ErrorHandler;
25 changes: 25 additions & 0 deletions src/helpers/errorHandler/errorRegistry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
type AbraError = {
template: string;
isDevOnly?: boolean;
isShowToUser?: boolean;
}

export const errorRegistry = {
GENERIC_ERROR: {
template: "Something went wrong.",
},

// Custom errors___________________
CUSTOM_TEST_ERROR: {
template: "Example of custom errors.",
},
//_________________________________

VIEM_ERROR: {
template: "{message}",
},

AXIOS_ERROR: {
template: "Axios error",
}
};