Optional.ofNullable(message.data()).ifPresent(ReferenceCountUtil::safestRelease); LOGGER.error("Failed to encode message: {}", message, ex); throw new MessageCodecException("Failed to encode message", ex);
@Override public String toString() { return getClass().getSimpleName() + "{errorMessage=" + getMessage() + '}'; } }
Optional.ofNullable(message.data()).ifPresent(ReferenceCountUtil::safestRelease); LOGGER.error("Failed to encode message: {}", message, ex); throw new MessageCodecException("Failed to encode message", ex);
/** * Decode buffers. * * @param dataBuffer the buffer of the data (payload) * @param headersBuffer the buffer of the headers * @return a new Service message with {@link ByteBuf} data and with parsed headers. * @throws MessageCodecException when decode fails */ public ServiceMessage decode(ByteBuf dataBuffer, ByteBuf headersBuffer) throws MessageCodecException { ServiceMessage.Builder builder = ServiceMessage.builder(); if (dataBuffer.isReadable()) { builder.data(dataBuffer); } if (headersBuffer.isReadable()) { try (ByteBufInputStream stream = new ByteBufInputStream(headersBuffer, true)) { builder.headers(headersCodec.decode(stream)); } catch (Throwable ex) { ReferenceCountUtil.safestRelease(dataBuffer); // release data buf as well throw new MessageCodecException("Failed to decode message headers", ex); } } return builder.build(); }
throw new MessageCodecException("Root should be object", null); throw new MessageCodecException("Failed to decode message", ex);
throw new MessageCodecException("Root should be object", null); throw new MessageCodecException("Failed to decode message", ex);
ReferenceCountUtil.safestRelease(dataBuffer); LOGGER.error("Failed to encode data on: {}, cause: {}", message, ex); throw new MessageCodecException( "Failed to encode data on message q=" + message.qualifier(), ex); throw new MessageCodecException( "Failed to encode headers on message q=" + message.qualifier(), ex);
/** * 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(); } }