Skip to content

Commit

Permalink
feat: let usecatcherror use the message returned by the backend (#69)
Browse files Browse the repository at this point in the history
* feat: let useCatchError use the message returned by the backend

* feat: let useCatchError use the message returned by the backend

* feat: let useCatchError use the message returned by the backend
  • Loading branch information
braghettos authored Apr 18, 2024
1 parent 3256a9a commit 5b13d9c
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/utils/useCatchError.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,25 @@ const useCatchError = () => {
let description: string = "Unable to complete the operation, please try later";

console.log("ERROR", error);
if (typeof error === "string") {
message = error;
} else if (error?.message) {
message = error.message;

// Adjust to account for potentially nested error structure
const actualError = error?.data || error;

console.log("actualERROR", actualError);

if (typeof actualError === "string") {
message = actualError;
} else if (actualError?.message) {
message = actualError.message;
}

if (error?.code) {
if (actualError?.code) {
const clientErrorRegex = /^4\d{2}$/; // Regex for 4xx client errors
if (clientErrorRegex.test(String(error.code))) {
if (clientErrorRegex.test(String(actualError.code))) {
message = "Client Error";
description = error.message || "There was an error processing your request. Please check your input or permissions.";
description = actualError.message || "There was an error processing your request. Please check your input or permissions.";
} else {
switch (error.code) {
switch (actualError.code) {
// Handle other specific codes if necessary
case 500:
message = "Internal Server Error";
Expand Down

0 comments on commit 5b13d9c

Please sign in to comment.