Kest

Search

Search the documentation

Microservices

Exception filters

Shape the error a caller receives.

An exception thrown by a @MessagePattern handler becomes an error reply. RpcException carries a status. The default answer is {"err": {"message": ..., "status": ...}}, which client.send raises as an RpcException:

@MessagePattern("charge") fun charge(req: ChargeRequest): Receipt {
    if (req.cents <= 0) throw RpcException("amount must be positive", 400)       // the caller gets this message and status
    return gateway.charge(req)
}
 
// the caller
try { client.send<Receipt>("charge", ChargeRequest(0)) } catch (e: RpcException) { println("${e.status}: ${e.message}") }
Thrown in the handlerThe caller gets
RpcException(msg, status) or an HttpExceptionthat message and status
anything elseInternal server error, status 500; the real error is logged and sent to error monitoring
no answer in time (caller side)RpcException with status 408

Attach an ExceptionFilter with @UseFilters to shape errors yourself (translate a domain exception into a status):

@UseFilters(PaymentErrors::class) @MessagePattern("charge") fun charge(req: ChargeRequest): Receipt = ...

See Exception filters. For events there is no caller to answer: a failing event handler is logged and reported, and (depending on the broker) the message may be redelivered.

Read more