So I just had a look at a fairly new restful api, written in kotlin.

The developers who made the api decided to be smart about error handling - instead of doing error handling in every api endpoint - why not just do some generic error handling.

The error handling exists in ControllerExceptionHandler.kt

The code would look something like:

    @ExceptionHandler(WebClientResponseException::class)
    fun handleWebClientResponseException(e: WebClientResponseException): ResponseEntity<Any> {
        logger().error("Error from WebClient - Status {}: {}", e.rawStatusCode, e.statusText, e)
        return ResponseEntity(mapOf("error" to "Error in external service"), HttpStatus.BAD_GATEWAY)
    }

That really seems like a smart thing to do - right!

Well I suppose you save a lot of time not doing exception handling on each and every endpoint.

However error handling is not something that should be generalized.

The thing is: when an error happens in one situation it should probably be handled completely differently from another situation.

For instance imagine a customer not being able to order a product on your website - you probably want customer support handing this ASAP.

On the other hand if you cannot load related products on the product page, you probably want to log an error and have developers look into it at next convenient time.

These are two examples that could easily both stem from the same WebClientResponseException - but they should be handled very differently.

Bottom line:

Exception handling is pretty important to do specifically and differently in every instance, since the response is probably different for different use cases.

And actually you most likely want to ask the business owners what should happen in each case.