20 Jul 2023 02:08 PM
When using the client-state SDK, what is the best way to see when a key is not set vs when there are other issues? Would the snippet below be the right answer?
Thanks!
return stateClient
.getAppState(...)
.then(...)
.catch((err) => {
if (isErrorResponseError(err) && err.response.status === 404) {
// key not set
} else {
throw err;
}
});
Solved! Go to Solution.
20 Jul 2023 03:47 PM - last edited on 20 Nov 2023 10:36 AM by stefan_eggersto
I'm not sure what the isErrorResponseError function looks like, but basically it looks correct and something as below would work
return stateClient
.getAppState(...)
.then(...)
.catch((err) => {
const status = e?.response?.status;
if (status && status === 404) {
// key not set
} else {
throw err;
}
});
Alternatively, if the non-existence of a certain queried app state is a genuine scenario that you want to cover in your app you could also handle this without explicit error handling logic (as explained in https://developer.dynatrace.com/develop/data/state/#filter-states ) via
const oneElementOrEmpty: Array<{ key: string, value: string }> = await stateClient.getAppStates({
filter: "key = 'might-not-exist'",
add-fields: "value",
});
and build your logic based on empty/non-empty-array result.
21 Jul 2023 09:38 AM - edited 21 Jul 2023 09:46 AM
Indeed, this seems to be the way to go in my case, as it would not require specific error handling. Thanks!
Edit: FYI, `isErrorResponseError` is exposed in the SDK itself, and it seemed like the way to go, but next time I will use the "simplified" condition or the solution you provided without the explicit error handling 🙂