0

Imagine that there is some manager class that talks to remote service, for example, to user microservice that can create new and update existing user profile. This manager class is used everywhere in the code: in controllers and other classes. Before talking to remote service, our manager class doesn't know if submitted DTO is valid. The question is: if remote service returns an validation errors, what to do next? How to handle this errors? I've thought about it, and have some options:

  • Throw an Exception when validation fails
  • Pass an Errors object that collects validation errors to the manager
  • make a method getLastErrors() in a manager class

Maybe there are other better solution exist?

p.s. Suppose that remote service returns errors in JSON format, it doesn't matter if it's JSON-RPC, SOAP or REST microservice.

Serhii Smirnov
  • 1,338
  • 1
  • 16
  • 24

2 Answers2

1

Unless you want to translate service errors into something different, or even handle them to take certain decisions in the client tier, usually service errors are formatted in a human-readable way to be shown in the UI to let the user know what went wrong.

In the other hand, if there's no UI, there should be a logger. Like you would do in a UI layer, you would format those errors log them to a file or any other storage approach.

Also, you might want to learn more about what's the fail-fast concept:

In systems design, a fail-fast system is one which immediately reports at its interface any condition that is likely to indicate a failure. Fail-fast systems are usually designed to stop normal operation rather than attempt to continue a possibly flawed process. Such designs often check the system's state at several points in an operation, so any failures can be detected early. A fail-fast module passes the responsibility for handling errors, but not detecting them, to the next-highest level of the system.

OP commented out this:

If validation errors are returned from the microservice, what manager class should do then? Throw an Exception or put these errors in some field in it's class?

About this concern, I've arrived to some conclusion, and it's that the entire flow should pass through a specialized DTO that I've called accumulated result (check the full description):

Represents a multi-purpose, orthogonal entity which transports both results of a called operation and also useful information for the callers like a status, status description and details about the actual result of the whole operation.

That way, even in multi-tier architectures, each tier/layer can either add more info to the accumulated result or take decisions.

Probably some may argue that you should throw exceptions, but I don't find that a broken rule is an exception but an expected use case.

Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206
0

how to handle validation errors from remote service?

Return the relevant HTTP status code, along with as much information as is necessary (sometimes none) in the response body.

It is not important if it's SOAP or RESTful, imagine that JSON response is returned

The type of service it is will determine what your failure handling approach will be. For SOAP services, you should return a SOAP fault.

tom redfern
  • 30,562
  • 14
  • 91
  • 126