From c9c116f7ab18a27fb3d6864e4ca95cf16e041066 Mon Sep 17 00:00:00 2001 From: Kevin He Date: Thu, 10 Oct 2024 08:59:58 -0700 Subject: [PATCH] add docs for error handling (#174) * Update README.md * Update README.md * Update README.md Co-authored-by: Alex Vuong --------- Co-authored-by: Alex Vuong --- README.md | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d6c035bc..8bb2767b 100644 --- a/README.md +++ b/README.md @@ -84,10 +84,35 @@ const config = { For more info, refer to the [documentation site](https://salesforcecommercecloud.github.io/commerce-sdk-isomorphic/). +#### `throwOnBadResponse` + +When `true`, the SDK throws an `Error` on responses whose status is not 2xx or 304. By default, the value of this flag is `false` for backwards compatibility. Below is an example for accessing the error object via `e.response.json()`. + +```js +const config = { + throwOnBadResponse: true + // rest of the config object... +}; + +const shopperSearch = new ShopperSearch({ + ...config +}); + +// in an async function +try { + const searchResult = await shopperSearch.productSearch({ + parameters: { q: "shirt" }, + }); +} catch (e) { + const error = await e.response.json(); + console.log(error); + // error is the JSON object - {error: ",,,"} +} +``` + #### Additional Config Settings * `headers`: Headers to include with API requests. -* `throwOnBadResponse`: When `true`, the SDK throws an `Error` on responses whose status is not 2xx or 304. ### Custom Query Parameters