Skip to content

Commit

Permalink
fix: update error catching to catch all produced errors
Browse files Browse the repository at this point in the history
  • Loading branch information
courcelan committed Aug 10, 2024
1 parent 6ede5e4 commit d91d47f
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 13 deletions.
4 changes: 2 additions & 2 deletions examples/hooks.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
}
)
.catch(
(error) => console.error("post: ", err.message, err.cause)
(err) => console.error("post: ", err.message, err.cause)
);


Expand All @@ -67,7 +67,7 @@
(res) => console.log(res)
)
.catch(
(error) => console.error("page: ", err.message, err.cause)
(err) => console.error("page: ", err.message, err.cause)
)
});
</script>
Expand Down
27 changes: 21 additions & 6 deletions lib/useButter.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,7 @@ export default function useButter(type, butterConfig) {
*/
catch (errorPayload) {
if (errorPayload.response) {
const {
detail: errorDetail
} = await errorPayload.response.json();
const errors = await errorPayload.response.json();

const mappedParams = Object.fromEntries(
Object.entries(errorPayload.params)
Expand All @@ -134,7 +132,7 @@ export default function useButter(type, butterConfig) {
url.searchParams.delete('auth_token');

const cause = {
data: errorDetail,
data: errors,
headers: errorPayload.response.headers,
status: errorPayload.response.status,
statusText: errorPayload.response.statusText,
Expand All @@ -145,13 +143,30 @@ export default function useButter(type, butterConfig) {
};

useOnError(
errorDetail,
errors,
cause
);

/**
* Formats error messages from an object of errors.
*
* @param {Object} unformattedErrors - An object containing error messages.
* @returns {string} A formatted error message string.
*/
function formatErrors (unformattedErrors) {
const errorKeys = Object.keys(unformattedErrors);

if (errorKeys.length > 1) {
return `Errors caught in [${ errorKeys.join(", ") }]: see cause for details`;
}
else {
return `[${ errorKeys[0]}]: ${ unformattedErrors[errorKeys[0]] }`;
}
}

return Promise.reject(
new Error(
`${ errorDetail } (${ errorPayload.response.status })`,
`${ formatErrors(errors) } (${ errorPayload.response.status })`,
{
cause
}
Expand Down
4 changes: 2 additions & 2 deletions tests/basic.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
}
);

// butter.content.cancelRequest()
butter.content.cancelRequest()

butter.post.list({
page: 1,
Expand Down Expand Up @@ -84,7 +84,7 @@
}
);

//butter.post.cancelRequest()
butter.post.cancelRequest()
});
</script>
</body>
Expand Down
4 changes: 2 additions & 2 deletions tests/hooks.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
}
)
.catch(
(error) => console.error("post: ", err.message, err.cause)
(err) => console.error("post: ", err.message, err.cause)
);


Expand All @@ -67,7 +67,7 @@
(res) => console.log(res)
)
.catch(
(error) => console.error("page: ", err.message, err.cause)
(err) => console.error("page: ", err.message, err.cause)
)
});
</script>
Expand Down
2 changes: 1 addition & 1 deletion tests/lib/butter.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d91d47f

Please sign in to comment.