/** * Returns error type. Error type is an identifier of a group of errors. * * @return error type. */ public int errorType() { if (!isError()) { throw new IllegalStateException("Message is not an error"); } try { return Integer.parseInt(Qualifier.getQualifierAction(qualifier())); } catch (NumberFormatException e) { throw new IllegalStateException("Error type must be a number"); } }
private ServiceMessage throwIfError(ServiceMessage message) { if (message.isError() && message.hasData(ErrorData.class)) { throw Exceptions.propagate(errorMapper.toError(message)); } return message; }
private GatewayMessage prepareResponse( Long streamId, ServiceMessage message, AtomicBoolean receivedErrorMessage) { GatewayMessage.Builder response = GatewayMessage.from(message).streamId(streamId); if (message.isError()) { receivedErrorMessage.set(true); response.signal(Signal.ERROR); } return response.build(); } }
private ServiceMessage throwIfError(ServiceMessage message) { if (message.isError() && message.hasData(ErrorData.class)) { throw Exceptions.propagate(errorMapper.toError(message)); } return message; }
private Mono<Void> handleRequest( ByteBuf content, HttpServerRequest httpRequest, HttpServerResponse httpResponse) { String qualifier = httpRequest.uri(); Builder builder = ServiceMessage.builder().qualifier(qualifier).data(content); enrichRequest(httpRequest.requestHeaders(), builder); return serviceCall .requestOne(builder.build()) .doOnNext(message -> metrics.markServiceResponse()) .switchIfEmpty( Mono.defer(() -> Mono.just(ServiceMessage.builder().qualifier(qualifier).build()))) .flatMap( response -> { enrichResponse(httpResponse, response); return Mono.defer( () -> response.isError() // check error ? error(httpResponse, response) : response.hasData() // check data ? ok(httpResponse, response) : noContent(httpResponse)); }); }
/** * Decode message. * * @param message the original message (with {@link ByteBuf} data) * @param dataType the type of the data. * @return a new Service message that upon {@link ServiceMessage#data()} returns the actual data * (of type data type) * @throws MessageCodecException when decode fails */ public static ServiceMessage decodeData(ServiceMessage message, Class<?> dataType) throws MessageCodecException { if (!message.hasData(ByteBuf.class) || dataType == null) { return message; } Object data; Class<?> targetType = message.isError() ? ErrorData.class : dataType; ByteBuf dataBuffer = message.data(); try (ByteBufInputStream inputStream = new ByteBufInputStream(dataBuffer, true)) { DataCodec dataCodec = DataCodec.getInstance(message.dataFormatOrDefault()); data = dataCodec.decode(inputStream, targetType); } catch (Throwable ex) { throw new MessageCodecException( "Failed to decode data on message q=" + message.qualifier(), ex); } return ServiceMessage.from(message).data(data).build(); } }