I have a rest api and a reactjs front end, in some cases, the api will send an error to the front end and I need to display that error. My app will support multiple languages, so the error must be translated, but what confuses me is how should I send that error so it can display the correct translation?
How should I do that? The back end should translate the error and send to the front end? The front end should get the error message and somehow translate it? Is there any pattern for this?
Best Answer
Error Codes
Essentially you need to mark every error with an error code, and that error code needs to have a language specific translation/format string.
The backend as such is unaware of language. Any error message generated alongside it is purely for internal consumption by dev and support.
Where the error code is translated into a consumer language specific error message depends on largely on taste.
I'd recommend two tables for translation.
Don't forget to have a general error, error so that new errors added by devs always have some presentation for the user.
As Ralf Kleberhoff has pointed out below, the Error Code does not need to be an integer. It can for example be a short string.
eg:
IRS_DISCOUNT_NOT_APPLICABLE
You don't even have to capitalise it as I did, but it is generally a good idea to have a naming convention.