Define error messages as exportable constants
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)
}
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)
}
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