@Override public ServiceMessage toMessage(Throwable throwable) { // implement service mapping logic if (throwable instanceof ServiceAException) { ServiceAException e = (ServiceAException) throwable; return ServiceMessage.error(BadRequestException.ERROR_TYPE, e.code(), e.getMessage()); } // or delegate it to default mapper return DefaultErrorMapper.INSTANCE.toMessage(throwable); } }
private void handleError(WebsocketSession session, GatewayMessage req, Throwable th) { LOGGER.error("Exception occurred on request: {}, session={}", req, session.id(), th); Builder builder = GatewayMessage.from(DefaultErrorMapper.INSTANCE.toMessage(th)); Optional.ofNullable(req.streamId()).ifPresent(builder::streamId); GatewayMessage response = builder.signal(Signal.ERROR).build(); session .send(response) .subscribe( null, throwable -> LOGGER.error( "Exception occurred on sending ERROR signal: {}, session={}", response, session.id(), throwable)); }
@Override public Publisher<Void> apply(HttpServerRequest httpRequest, HttpServerResponse httpResponse) { LOGGER.debug( "Accepted request: {}, headers: {}, params: {}", httpRequest, httpRequest.requestHeaders(), httpRequest.params()); if (httpRequest.method() != POST) { LOGGER.error("Unsupported HTTP method. Expected POST, actual {}", httpRequest.method()); return methodNotAllowed(httpResponse); } return httpRequest .receive() .aggregate() .map(ByteBuf::retain) .doOnNext(content -> metrics.markRequest()) .flatMap(content -> handleRequest(content, httpRequest, httpResponse)) .doOnSuccess(avoid -> metrics.markResponse()) .onErrorResume(t -> error(httpResponse, DefaultErrorMapper.INSTANCE.toMessage(t))); }