-
Notifications
You must be signed in to change notification settings - Fork 130
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
[FEATURE] Add more details about API error #1659
Comments
Thanks @LucasReyre. Using SCAPI hooks to add details to error message with Script API's If you drop down to the level of I'm curious about where in the stack you've observed the details being lost. Thanks! |
To build on the issue, To replicate ...
// dw.ocapi.shop.category.beforeGET
exports.beforeGET = function errorWithDetails() {
const status = new Status(Status.ERROR)
status.addDetail("validation_error", "Naughty Category")
return status
}
import capi from "commerce-sdk-isomorphic"
function getClientConfig() {
return {
parameters: {
shortCode: "kv7kzm78",
organizationId: "f_ecom_zzrf_016",
siteId: "RefArch",
clientId: "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa",
},
throwOnBadResponse: true,
}
}
async function main() {
const clientConfig = getClientConfig()
const shopperLoginClient = new capi.ShopperLogin(clientConfig)
// 1️⃣ Login as a guest.
const guestTokenResponse = await capi.helpers.loginGuestUserPrivate(
shopperLoginClient,
{},
{ clientSecret: "REDACTED" }
)
clientConfig.headers = {
Authorization: `Bearer ${guestTokenResponse.access_token}`,
}
// 2️⃣ Make a call to categories which throws an error.
try {
await new capi.ShopperProducts(clientConfig).getCategory({
parameters: { id: "root", levels: 0 },
})
} catch (err) {
console.error({ errData: await err.response.json() })
}
}
main() |
Hi @LucasReyre @johnboxall ! Thank you for bringing this up. To build on John's point, you can grab error from the Yes, there is a way to access API errors!There is a way to grab errors and althou it isn't the prettiest way. const product = useProduct(
{parameters: {id: '25502228Mxxx'}},
{
onError: async (error) => {
// access error properties in json
const json = await error.response.json()
console.log(json)
}
}
) There is a playground app where you can reproduce this -> https://github.com/SalesforceCommerceCloud/pwa-kit/compare/demo-commerce-sdk-react-error-handling?expand=1 But, it should be better!As you may already found out, the above method uses a callback function that doesn't play well with react states. Ideally it should be very easy to access errors like this: const product = useProduct({parameters: {id: '25502228Mxxx'}})
console.log(product.error) I'm going to create a bug ticket in our system to fix this issue. In the meantime, please use the callback method to access error. Thank you! |
Hello @kevinxh The way you catch the error details works well, I will use it until you release something easier. Thanks to you two |
Is your feature request related to a problem? Please describe.
It seems that there is currently no way to retrieve the body of an API request in error.
This is a real pain point as I have customized some SCAPI endpoint and return custom status code like :
And I would like to display custom messages on the front-end depending on this statusCode.
Describe the solution you'd like
I would like the possibility to get back the API's body from the SDK.
The text was updated successfully, but these errors were encountered: