List of usage examples for org.apache.thrift TApplicationException TApplicationException
public TApplicationException(int type, String message)
From source file:com.facebook.swift.service.ThriftMethodHandler.java
License:Apache License
private Object readResponse(TProtocol in) throws Exception { TProtocolReader reader = new TProtocolReader(in); reader.readStructBegin();// www. j av a 2s . com Object results = null; Exception exception = null; while (reader.nextField()) { if (reader.getFieldId() == 0) { results = reader.readField(successCodec); } else { ThriftCodec<Object> exceptionCodec = exceptionCodecs.get(reader.getFieldId()); if (exceptionCodec != null) { exception = (Exception) reader.readField(exceptionCodec); } else { reader.skipFieldData(); } } } reader.readStructEnd(); in.readMessageEnd(); if (exception != null) { throw exception; } if (successCodec.getType() == ThriftType.VOID) { // TODO: check for non-null return from a void function? return null; } if (results == null) { throw new TApplicationException(TApplicationException.MISSING_RESULT, name + " failed: unknown result"); } return results; }
From source file:com.facebook.swift.service.ThriftMethodHandler.java
License:Apache License
private void waitForResponse(TProtocol in, int sequenceId) throws TException { TMessage message = in.readMessageBegin(); if (message.type == EXCEPTION) { TApplicationException exception = TApplicationException.read(in); in.readMessageEnd();//from www . j a va 2 s . com throw exception; } if (message.type != REPLY) { throw new TApplicationException(INVALID_MESSAGE_TYPE, "Received invalid message type " + message.type + " from server"); } if (!message.name.equals(this.name)) { throw new TApplicationException(WRONG_METHOD_NAME, "Wrong method name in reply: expected " + this.name + " but received " + message.name); } if (message.seqid != sequenceId) { throw new TApplicationException(BAD_SEQUENCE_ID, name + " failed: out of sequence response"); } }
From source file:com.facebook.swift.service.ThriftMethodProcessor.java
License:Apache License
private Object[] readArguments(TProtocol in) throws Exception { try {//www.ja va 2s .co m int numArgs = method.getParameterTypes().length; Object[] args = new Object[numArgs]; TProtocolReader reader = new TProtocolReader(in); // Map incoming arguments from the ID passed in on the wire to the position in the // java argument list we expect to see a parameter with that ID. reader.readStructBegin(); while (reader.nextField()) { short fieldId = reader.getFieldId(); ThriftCodec<?> codec = parameterCodecs.get(fieldId); if (codec == null) { // unknown field reader.skipFieldData(); } else { // Map the incoming arguments to an array of arguments ordered as the java // code for the handler method expects to see them args[thriftParameterIdToJavaArgumentListPositionMap.get(fieldId)] = reader.readField(codec); } } reader.readStructEnd(); // Walk through our list of expected parameters and if no incoming parameters were // mapped to a particular expected parameter, fill the expected parameter slow with // the default for the parameter type. int argumentPosition = 0; for (ThriftFieldMetadata argument : parameters) { if (args[argumentPosition] == null) { Type argumentType = argument.getThriftType().getJavaType(); if (argumentType instanceof Class) { Class<?> argumentClass = (Class<?>) argumentType; argumentClass = Primitives.unwrap(argumentClass); args[argumentPosition] = Defaults.defaultValue(argumentClass); } } argumentPosition++; } return args; } catch (TProtocolException e) { // TProtocolException is the only recoverable exception // Other exceptions may have left the input stream in corrupted state so we must // tear down the socket. throw new TApplicationException(TApplicationException.PROTOCOL_ERROR, e.getMessage()); } }
From source file:com.facebook.swift.service.ThriftServiceProcessor.java
License:Apache License
public static TApplicationException createAndWriteApplicationException(TProtocol outputProtocol, RequestContext requestContext, String methodName, int sequenceId, int errorCode, String errorMessage, Throwable cause) throws TException { // unexpected exception TApplicationException applicationException = new TApplicationException(errorCode, errorMessage); if (cause != null) { applicationException.initCause(cause); }/*www .ja v a 2s. c o m*/ LOG.error(applicationException, errorMessage); return writeApplicationException(outputProtocol, requestContext, methodName, sequenceId, applicationException); }
From source file:com.flipkart.phantom.thrift.impl.ProxyServiceClient.java
License:Apache License
/** * Overriden super class method. Receives the service response and relays it back to the client * @see org.apache.thrift.TServiceClient#receiveBase(org.apache.thrift.TBase, String) */// w w w . ja v a2s .c om public void receiveBase(TBase result, String methodName) throws TException { // Read the service response - same as in TServiceClient#receiveBase TMessage msg = iprot_.readMessageBegin(); if (msg.type == TMessageType.EXCEPTION) { TApplicationException x = TApplicationException.read(iprot_); iprot_.readMessageEnd(); throw x; } if (msg.seqid != this.seqid_) { throw new TApplicationException(TApplicationException.BAD_SEQUENCE_ID, methodName + " failed: out of sequence response"); } result.read(iprot_); iprot_.readMessageEnd(); // now relay the response to the client clientProtocol.writeMessageBegin(msg); result.write(clientProtocol); clientProtocol.writeMessageEnd(); clientProtocol.getTransport().flush(); LOGGER.debug("Relayed thrift response to client. Seq Id : " + msg.seqid + ", Method : " + msg.name + ", value : " + result); }
From source file:com.linecorp.armeria.client.thrift.ThriftClientCodec.java
License:Apache License
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override/*from ww w . jav a2 s. co m*/
public <T> T decodeResponse(ServiceInvocationContext ctx, ByteBuf content, Object originalResponse)
throws Exception {
if (content == null) {
return null;
}
if (!content.isReadable()) {
ThriftMethod thriftMethod = getThriftMethod(ctx);
if (thriftMethod != null && thriftMethod.isOneWay()) {
return null;
}
throw new TApplicationException(TApplicationException.MISSING_RESULT, ctx.toString());
}
TByteBufInputTransport inputTransport = new TByteBufInputTransport(content);
TProtocol inputProtocol = protocolFactory.getProtocol(inputTransport);
TMessage msg = inputProtocol.readMessageBegin();
if (msg.type == TMessageType.EXCEPTION) {
TApplicationException ex = TApplicationException.read(inputProtocol);
inputProtocol.readMessageEnd();
throw ex;
}
ThriftMethod method = methodMap.get(msg.name);
if (method == null) {
throw new TApplicationException(TApplicationException.WRONG_METHOD_NAME, msg.name);
}
TBase<? extends TBase, TFieldIdEnum> result = method.createResult();
result.read(inputProtocol);
inputProtocol.readMessageEnd();
for (TFieldIdEnum fieldIdEnum : method.getExceptionFields()) {
if (result.isSet(fieldIdEnum)) {
throw (TException) result.getFieldValue(fieldIdEnum);
}
}
TFieldIdEnum successField = method.successField();
if (successField == null) { //void method
return null;
}
if (result.isSet(successField)) {
return (T) result.getFieldValue(successField);
}
throw new TApplicationException(TApplicationException.MISSING_RESULT,
result.getClass().getName() + '.' + successField.getFieldName());
}
From source file:com.linecorp.armeria.client.thrift.THttpClientDelegate.java
License:Apache License
private static TApplicationException readApplicationException(ThriftFunction func, TProtocol inputProtocol, TMessage msg) throws TException { final TApplicationException appEx; if (msg.type == TMessageType.EXCEPTION) { appEx = TApplicationException.read(inputProtocol); inputProtocol.readMessageEnd();// ww w .j a v a 2 s . c o m } else if (!func.name().equals(msg.name)) { appEx = new TApplicationException(TApplicationException.WRONG_METHOD_NAME, msg.name); } else { appEx = null; } return appEx; }
From source file:com.linecorp.armeria.common.thrift.text.TTextProtocolTest.java
License:Apache License
@Test public void rpcTApplicationException() throws Exception { String request = "{\n" + " \"method\" : \"doDebug\",\n" + " \"type\" : \"EXCEPTION\",\n" + " \"seqid\" : 101,\n" + " \"args\" : {\n" + " \"type\" : 4,\n" + " \"message\" : \"bad_seq_id\"\n" + " }\n" + " }\n" + '}'; TTextProtocol prot = new TTextProtocol( new TIOStreamTransport(new ByteArrayInputStream(request.getBytes()))); TMessage header = prot.readMessageBegin(); TApplicationException result = TApplicationException.read(prot); prot.readMessageEnd();// ww w . ja v a 2 s. c om assertEquals("doDebug", header.name); assertEquals(TMessageType.EXCEPTION, header.type); assertEquals(101, header.seqid); assertEquals(TApplicationException.BAD_SEQUENCE_ID, result.getType()); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); prot = new TTextProtocol(new TIOStreamTransport(outputStream)); prot.writeMessageBegin(header); new TApplicationException(TApplicationException.BAD_SEQUENCE_ID, "bad_seq_id").write(prot); prot.writeMessageEnd(); assertJsonEquals(request, new String(outputStream.toByteArray(), StandardCharsets.UTF_8)); }
From source file:com.linecorp.armeria.internal.thrift.TApplicationExceptions.java
License:Apache License
/** * Reads a {@link TApplicationException} from the specified {@link TProtocol}. * * <p>Note: This has been copied from {@link TApplicationException#read(TProtocol)} due to API differences * between libthrift 0.9.x and 0.10.x./*from ww w. j a v a2 s . co m*/ */ public static TApplicationException read(TProtocol iprot) throws TException { TField field; iprot.readStructBegin(); String message = null; int type = TApplicationException.UNKNOWN; while (true) { field = iprot.readFieldBegin(); if (field.type == TType.STOP) { break; } switch (field.id) { case 1: if (field.type == TType.STRING) { message = iprot.readString(); } else { TProtocolUtil.skip(iprot, field.type); } break; case 2: if (field.type == TType.I32) { type = iprot.readI32(); } else { TProtocolUtil.skip(iprot, field.type); } break; default: TProtocolUtil.skip(iprot, field.type); break; } iprot.readFieldEnd(); } iprot.readStructEnd(); return new TApplicationException(type, message); }
From source file:com.linecorp.armeria.internal.thrift.ThriftFunction.java
License:Apache License
/** * Converts the specified {@code result} into a Java object. *//*from ww w . j av a2 s . c o m*/ public Object getResult(TBase<TBase<?, ?>, TFieldIdEnum> result) throws TException { for (TFieldIdEnum fieldIdEnum : exceptionFields()) { if (result.isSet(fieldIdEnum)) { throw (TException) ThriftFieldAccess.get(result, fieldIdEnum); } } final TFieldIdEnum successField = successField(); if (successField == null) { //void method return null; } else if (result.isSet(successField)) { return ThriftFieldAccess.get(result, successField); } else { throw new TApplicationException(TApplicationException.MISSING_RESULT, result.getClass().getName() + '.' + successField.getFieldName()); } }