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

add in full cause to thrown errors #94

Merged
merged 2 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion __tests__/butter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ test(
catch (error) {
await expect(error).toBeDefined();
await expect(error).toBeInstanceOf(Error);
await expect(error.message).toEqual("Error: Not found");
await expect(error.message).toEqual("Error: Not found (404)");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have one error while running tests https://share.zight.com/L1uEo6mm

}

return
Expand Down
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)
marcinpece-apptension marked this conversation as resolved.
Show resolved Hide resolved
);


Expand All @@ -67,7 +67,7 @@
(res) => console.log(res)
)
.catch(
(error) => console.error("page: ", error)
(error) => console.error("page: ", err.message, err.cause)
marcinpece-apptension marked this conversation as resolved.
Show resolved Hide resolved
)
});
</script>
Expand Down
32 changes: 26 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In some cases error detail might be not in detail prop. Should we cover somehow or current state is ok?
https://share.zight.com/8LuNRWmL

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for this @marcinpece-apptension, I've updated the code to handle a more general set of error keys/values. Please give this all another check.

} = 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 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()
marcinpece-apptension marked this conversation as resolved.
Show resolved Hide resolved

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)
marcinpece-apptension marked this conversation as resolved.
Show resolved Hide resolved
);


Expand All @@ -67,7 +67,7 @@
(res) => console.log(res)
)
.catch(
(error) => console.error("page: ", error)
(error) => console.error("page: ", err.message, err.cause)
marcinpece-apptension marked this conversation as resolved.
Show resolved Hide resolved
)
});
</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.