How to idiomatically handle exceptions in Spring Boot?

I’m lost when it comes to handling exceptions in Spring Boot; I know the tools, I know the functions, I just have no idea how to use them effectively, even less so after today’s code review.

Specifically:

  • does it make sense to leverage ResponseStatusExceptionResolver? Personally – I don’t think so; why should exception be tied to e.g. status codes at time of definition?
  • where should I throw them – should I keep them in the service layer, or is the controller layer supposed to know about them? Does one of those approaches improve testability?
  • what should I return from my custom handler? I thought of leveraging handleExceptionInternal(...) in order to perform final code checks, but returning a nullable entity with Any inside gives me shivers. What is it, then? ProblemDetail? ResponseEntity<...>?
  • how do I test all of this in a concise manner? Is testing the controller itself enough? Can that mean I can enforce a stricter access modifier on the exception handler method?

My take on this is as follows:

import java.lang.RuntimeException
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpStatus
import org.springframework.http.ProblemDetail
import org.springframework.http.ResponseEntity
import org.springframework.stereotype.Service
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.bind.annotation.RestControllerAdvice
import org.springframework.web.context.request.WebRequest
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler

class CustomException : RuntimeException("Something happened")

@Service
class CustomService {
    fun getSomething() {
        throw CustomException()
    }
}

@RestController
@RequestMapping("/")
class CustomController(private val service: CustomService) {
    @GetMapping("/something")
    fun getSomething() {
        service.getSomething()
    }
}

@RestControllerAdvice
class CustomExceptionHandler : ResponseEntityExceptionHandler() {
    @ExceptionHandler(CustomException::class)
    fun handleCustomException(exception: CustomException, request: WebRequest): ResponseEntity<Any>? {
        val status = HttpStatus.NOT_FOUND
        val body = ProblemDetail.forStatusAndDetail(status, exception.message)
        return handleExceptionInternal(exception, body, HttpHeaders(), status, request)
    }
}

That is:

  • custom exception without any fancy annotations – just the message; doesn’t know about HTTP
  • thrown from the service layer; the controller layer has no idea this can happen
  • global @ExceptionHandler using @ControllerAdvice acts as a single component
  • handler method wraps the exception in a ProblemDetail, then leverages handleExceptionInternal

Is there a better way?

I’ve read similar posts, but I believe none of them answer my question in a satisfying manner; there’s no consensus what responsibilities each layer should have, which mechanism to use and so on.

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