Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 노드간 통신
- 개발 어렵당.ㅠ
- LPOS
- formik
- 애자일 싫타
- RedirectService
- 핀포인트
- Ingress Controller Fake
- Armeria
- OIDC
- 논블록킹 성능
- Loki 로그
- 오블완
- 월급루팡 일지
- 티스토리챌린지
- jar 배포
- fake jwt
- hbase 저장공간 설정
- intellij
- pinpoint 2.5.3
- nGinder
- UnsupportedOperationException
- pinpoint
- R2DBC Paging
- 7879
- jsonMarshaller
- 플루터
- save/update
- ㅉ때
- reids
Archives
- Today
- Total
대머리개발자
아르메리아(Armeria) - ExceptionHandler 본문
728x90
어노테이션을(@)을 이용해서 간단하게 적용할 수 있다.
import com.linecorp.armeria.server.annotation.*
@RestController
@ExceptionHandler(DefinedExceptionHandler::class)
@PathPrefix("/codes")
class CodeEndPoint @Autowired constructor(var codeService: CodeService){
@ProducesJson
@Get("/{codes}")
@Description("쿠폰 발행")
fun publishCodes(@Param("codes") @Description("쿠폰 코드")codes:String): Mono<ApiResponse> {
return codeService.publishCodes(codes)
}
...
}
ExceptionHandler는 ExceptionHandlerFunction을 구현하면 된다.
class DefinedExceptionHandler : ExceptionHandlerFunction {
override fun handleException(ctx: ServiceRequestContext, req: HttpRequest, cause: Throwable): HttpResponse {
val gson = Gson()
val mutableMap = mapOf(
"code" to "PRO500",
"message" to cause.message,
)
return HttpResponse.of(HttpStatus.INTERNAL_SERVER_ERROR, MediaType.JSON_UTF_8,
gson.toJson(mutableMap)
)
}
}
★전역으로 예외를 기술하는 이유는 내가 정의된 예외 말고 시스템에서 발생하는 오류를 한방에 이쁘게 처리하기 위함이다. ★
: 전역으로 처리 하지 않았을 경우는 내부 오류를 그대로 노출 된다. why? 예외를 처리 못했으니.
: 전역처리를 하면 예상하지 못한 오류를 catch해서 이쁘게 노출할 수 있다.
물론 내가 예상한 예외는 별도로 정의를 해두고 쓰고 있다.
그 외 예기치 못한 상황에서 발생되는 친구들은 전역에서 "PRO500"으로 퉁 친다.
...
fun duplicateError(msg:String): Mono<ApiResponse>
= Mono.just(ApiResponse(code = "PRO0200", message = "It is duplicate.($msg)"))
fun alreadyIssuedError(): Mono<ApiResponse>
= Mono.just(ApiResponse(code = "PRO0201", message = "This code has already been issued."))
fun invalidError(msg:String): Mono<ApiResponse>
= Mono.just(ApiResponse(code = "PRO0300", message = "It is not valid.($msg)"))
...
728x90
'개발이야기 > 코틀린' 카테고리의 다른 글
아르메리아를 쓰자. (1) | 2024.03.15 |
---|---|
ModelMapper - LocalDateTime처리 (1) | 2024.01.29 |
아르메리아 오류인듯...아닌듯. (1) | 2023.12.18 |
이벤트 서버의 성능 테스트 시작(2) (0) | 2023.11.22 |
이벤트 서버의 성능 테스트 시작(1) (1) | 2023.11.20 |