I am in two minds about sharing client / server errors codes. We are writing both ends of the system, if our API was external this would not be possible, but that requirement is very unlikely.

So an idea would be to have some shared directory between client and server and then on the server:

res.send(4xx).json({ error: CODES.FOO_CODE })

Whilst on the client:

catch (e) {
  if (e.data.code === CODES.FOO_CODE) {
    console.log('We don't have FOO')
  store.dispatch(notification, Unexpected error)

Is this considered a bad or good idea?

  • 2
    As Doc Brown points out below, such sharing causes coupling between the client and the server. In my opinion, the marginal benefit obtained by sharing error codes that are likely to have not that much in common is outweighed by the coupling cost. Dec 21 '21 at 15:27

If your frontend and your backend

  • are developed in the same programming language (typically Javascript)

  • are developed by the same team

then there is nothing generally wrong in having shared libraries for both, especially when there are things you want to keep in sync between frontend and backend. These libs can contain things as simple as error codes, but also more sophisticated artifacts like common datatypes, or validation logic you want to be executed for performance reasons in the frontend, but for security reasons also in the backend.

Be aware, however, whenever you reuse libs on both client and server, you will be exposing things from the server side to the public-facing client side. So you need to be careful what you are putting into such a common lib. This may be more a psychological than a technical issue, but to my experience, when backend code and frontend are strictly separated, the risk of exposing confidential internals is a lot lower.

Moreover, when your frontend is potentially deployed independently from your backend (like it often happens for mobile or desktop systems), then you have to take care that your software does not rely on both sides always using the same shared library version at all times. That situation isn't really different from the case where frontend and backend are developed mostly independently, by different teams and/or with different tools.


If you want to communicate specific error situations from the server to the client, they need to agree on their encoding. Effectively, it becomes part of the interface.

So, I'd place the error codes specification along the other interface definition parts (maybe you're using OpenAPI, then you can specify the error codes as enum values in the error-result description).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.