Search in sources :

Example 1 with ErrorResponse

use of com.faforever.server.error.ErrorResponse in project faf-java-server by FAForever.

the class ErrorResponseTransformerTest method transform.

@Test
public void transform() throws Exception {
    ErrorCode errorCode = ErrorCode.UNSUPPORTED_REQUEST;
    ErrorResponse errorResponse = new ErrorResponse(errorCode, UUID.randomUUID(), new Object[] { "foobar" });
    Map<String, Serializable> result = ErrorResponseTransformer.INSTANCE.transform(errorResponse);
    assertThat(result.get("command"), is("notice"));
    assertThat(result.get("style"), is("error"));
    assertThat(result.get("code"), is(errorCode.getCode()));
    assertThat(result.get("text"), is(MessageFormat.format(errorCode.getTitle() + ": " + errorCode.getDetail(), "foobar")));
}
Also used : Serializable(java.io.Serializable) ErrorCode(com.faforever.server.error.ErrorCode) ErrorResponse(com.faforever.server.error.ErrorResponse) Test(org.junit.Test)

Example 2 with ErrorResponse

use of com.faforever.server.error.ErrorResponse in project faf-java-server by FAForever.

the class IntegrationConfig method requestExceptionTransformer.

/**
 * Transformer that transforms a {@link MessageHandlingException} into and {@link ErrorResponse}.
 */
private AbstractTransformer requestExceptionTransformer() {
    return new AbstractTransformer() {

        @Override
        protected Object doTransform(Message<?> message) throws Exception {
            MessagingException messageException = (MessagingException) message.getPayload();
            Message<?> failedMessage = messageException.getFailedMessage();
            // TODO try to make more intuitive? Meanwhile, look at requestExceptionFilter() to understand.
            RequestException requestException;
            if (messageException instanceof MessageTransformationException) {
                requestException = (RequestException) messageException.getCause().getCause();
            } else {
                requestException = (RequestException) messageException.getCause();
            }
            MessageBuilder<ErrorResponse> builder = MessageBuilder.withPayload(new ErrorResponse(requestException.getErrorCode(), requestException.getRequestId(), requestException.getArgs())).copyHeaders(message.getHeaders());
            builder.setHeader(CLIENT_CONNECTION, failedMessage.getHeaders().get(CLIENT_CONNECTION, ClientConnection.class));
            return builder.build();
        }
    };
}
Also used : AbstractTransformer(org.springframework.integration.transformer.AbstractTransformer) MessageTransformationException(org.springframework.integration.transformer.MessageTransformationException) IceMessage(com.faforever.server.ice.IceMessage) Message(org.springframework.messaging.Message) MessagingException(org.springframework.messaging.MessagingException) ClientConnection(com.faforever.server.client.ClientConnection) RequestException(com.faforever.server.error.RequestException) ErrorResponse(com.faforever.server.error.ErrorResponse)

Example 3 with ErrorResponse

use of com.faforever.server.error.ErrorResponse in project faf-java-server by FAForever.

the class V2ServerMessageTransformerTest method errorMessage.

@Test
public void errorMessage() throws Exception {
    UUID requestId = UUID.randomUUID();
    String response = instance.transform(new ErrorResponse(ErrorCode.UNSUPPORTED_REQUEST, requestId, new String[] { "{\"foo\": \"bar\"}" }));
    assertThat(response, is("{\"data\":{\"code\":109,\"title\":\"Unsupported request\",\"text\":\"The server received an unsupported request from your client: {\\\"foo\\\": \\\"bar\\\"}\",\"requestId\":\"" + requestId + "\",\"args\":[\"{\\\"foo\\\": \\\"bar\\\"}\"]},\"type\":\"error\"}"));
}
Also used : UUID(java.util.UUID) ErrorResponse(com.faforever.server.error.ErrorResponse) Test(org.junit.Test)

Aggregations

ErrorResponse (com.faforever.server.error.ErrorResponse)3 Test (org.junit.Test)2 ClientConnection (com.faforever.server.client.ClientConnection)1 ErrorCode (com.faforever.server.error.ErrorCode)1 RequestException (com.faforever.server.error.RequestException)1 IceMessage (com.faforever.server.ice.IceMessage)1 Serializable (java.io.Serializable)1 UUID (java.util.UUID)1 AbstractTransformer (org.springframework.integration.transformer.AbstractTransformer)1 MessageTransformationException (org.springframework.integration.transformer.MessageTransformationException)1 Message (org.springframework.messaging.Message)1 MessagingException (org.springframework.messaging.MessagingException)1