Alternatives of handling Java exceptions message on REST microservices?

Context: Java SpringBoot exposing REST apis

I have a RestControllerAdvice that is used to return a Response with the message of the exceptions that may occur during business logic:

@Slf4j
@RestControllerAdvice
@RequiredArgsConstructor
public class ExceptionRestController {

    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler({ FunctionalRuntimeException.class })
    public DavinciApiResponse<Void> handleFunctionalRuntimeException(FunctionalRuntimeException exception) {
        DavinciApiResponse<Void> response = ExceptionUtils.buildResponse(exception);
        ExceptionUtils.logException(exception);
        return response;
    }

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler({ ValidationException.class,
                        ConstraintViolationException.class })
    public <E extends Exception> DavinciApiResponse<Void> handleValidationException(E exception) {
        DavinciApiResponse<Void> response = ExceptionUtils.buildResponse(exception);
        ExceptionUtils.logException(exception);
        return response;
    }

    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(Exception.class)
    public DavinciApiResponse<Void> handleAllUncaughtException(Exception exception) {
        DavinciApiResponse<Void> response = ExceptionUtils.buildResponse(exception);
        ExceptionUtils.logException(exception);
        return response;
    }

}

Should the exception message be used by the frontend team to show it to the final user?
From my little working experience I would say yes (this is the approach that I have always seen), but I don’t really agree with it…

Example 1:
I need to develop a multi-language Web application (ENGLISH – FRANCH – SPANISH)
Should backend be language aware ?
I’m pretty sure the answer is NO. It is a frontend responsibility to show the page in the language specified by the user, so it is his job to show an appropriate error message to the user.

Example 2:
Backend is using open-source libraries that throws RuntimeExceptions.
In this case it is also not possible to manipulate the exception message directly, at most I have to wrap it by passing it as argument to a new Exception.

A possible solution I was thinking for handling multi language exception message is to create a new Microservice for handling Business Logic exceptions:

  1. frontend call API of Microservice A
  2. a business logic exception was thrown on the API, so it returns as response an object containing a messageCode (UNIQUE CODE for identifying the message), and messageParams (parameters of the message code)
  3. the frontend then call API of “Microservice Business Logic exceptions” passing as request an object containing messageCode, messageParams and the language
  4. the “Microservice Business Logic exceptions” returns the exception message translated to the required language, for example by extracting the message from database passing the messageCode.

How do you handle the exceptions message ?
What are the alternatives ?

Errors are as much part of your REST API as non-error responses are. And that goes all the way: The error behaviour of your java methods is just as much part of your java API as non-error responses are; it’s just that folks find it difficult to think about error conditions and get lazy and then think the problem is ‘checked exceptions’ as a concept instead of writing a proper API. Hence, lots of tutorials treat exceptions as an after thought.

Create a proper (possibly hierarchical) description of all errors and act accordingly. The type of the exception is fair game for your API to send back. The actual text meant for human eyeballs indeed isn’t; that’s the front-end’s job.

Your job as backend dev is to explicitly list each and every error that can occur, why/when they occur, how your API responds, and to add any detail the front end might want about the error to the error response.

Depending on how much you are a ‘true believer’ in the REST concept, you should think about the correct HTTP error code to use for all these errors. If it’s a problem that is primarily an issue on your server, it should be some 5xx error. If the problem is primarily with the client’s request, it should be a 4xx, and so on.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật