Skip to content

Commit

Permalink
add in full cause to thrown errors
Browse files Browse the repository at this point in the history
removes auth_token from reporting
  • Loading branch information
courcelan committed Aug 6, 2024
1 parent b0a08ae commit 6a56058
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 15 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: ", error)
(error) => console.error("post: ", err.message, err.cause)
);


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

const mappedParams = Object.fromEntries(
Object.entries(errorPayload.params)
.filter(([key]) => key !== 'auth_token')
)

const url = new URL(errorPayload.response.url)

// Remove auth_token from URL search params
url.searchParams.delete('auth_token');

const cause = {
data: errorDetail,
headers: errorPayload.response.headers,
status: errorPayload.response.status,
statusText: errorPayload.response.statusText,
config: errorPayload.config,
params: mappedParams,
type,
url
}

useOnError(
errorDetail,
{
config: errorPayload.config,
params: errorPayload.params,
type
}
cause
);

return Promise.reject(
new Error(
errorDetail
`${ errorDetail } (${ errorPayload.response.status })`,
{
cause
}
)
);
} else {
Expand All @@ -154,6 +174,14 @@ export default function useButter(type, butterConfig) {

cleanup();

console.error(
"reportableError",
reportableError,
new Error(
reportableError
)
)

return Promise.reject(
new Error(
reportableError
Expand Down
8 changes: 4 additions & 4 deletions tests/basic.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@
// add the catch to ensure you do not receive a "uncaught (in promise) warning"
.catch(
(err) => {
console.error(err)
console.error(err.message, err.cause)
}
);

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

butter.post.list({
page: 1,
Expand Down Expand Up @@ -80,11 +80,11 @@
// add the catch to ensure you do not receive a "uncaught (in promise) warning"
.catch(
(err) => {
console.error(err)
console.error(err.message, err.cause)
}
);

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: ", error)
(error) => console.error("post: ", err.message, err.cause)
);


Expand All @@ -67,7 +67,7 @@
(res) => console.log(res)
)
.catch(
(error) => console.error("page: ", error)
(error) => 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 6a56058

Please sign in to comment.