cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

client-state SDK: informations about error being thrown

david_petroni
Dynatrace Enthusiast
Dynatrace Enthusiast

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;
        }
      });

 

 

 

 

2 REPLIES 2

gregor_riegler
Dynatrace Advocate
Dynatrace Advocate

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.

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 🙂

Featured Posts