sd-jwt-js icon indicating copy to clipboard operation
sd-jwt-js copied to clipboard

Define error messages as exportable constants

Open cre8 opened this issue 1 year ago • 2 comments

Sometimes we need to find out which error was thrown. To avoid a break of our application when we change the string of the error message, we should use constants that we use for throwing an error and that can be used by a developper for checking it like

const STATUS_NOT_VALID = "Status is not valid";
...
throw new Error(STATUS_NOT_VALID)
...
catch(e) {
if(e.message === STATUS_NOT_VALID)
}

cre8 avatar May 21 '24 11:05 cre8

It's a good idea.

Since the error message variables may be the same, let's include the namespace with it. such as:

catch(e) {
    if(e.message === SD_JWT_ERROR.STATUS_NOT_VALID)
}

lukasjhan avatar May 29 '24 05:05 lukasjhan

I think we could use specific error class for this right? I generally prefer that since we can add context to the errors:

if (error instanceof SdJwtException) {
 // error.code -> could be e.g. `STATUS_NOT_VALID` (typed value)
}

Could even have specific errors:

if (error instanceof SdJwtStatusException) {
  // error.reason -> could be same error as standard jwt error, so expired, not valid yet, but also for example NOT_RESOLVEABLE in case the fetch failed
}

I think this adds an easy way to programatically hook into the errors, while also allowing to keep the eror messages human readable and provide context

TimoGlastra avatar Oct 06 '25 16:10 TimoGlastra