In DeviceApiController reducing the number of error messages with stack trace
Signed-off-by: Oleksandra Matviienko <al.zzzeebra@gmail.com>
This commit is contained in:
parent
cb9236ff78
commit
d66dff3c6c
@ -16,6 +16,7 @@
|
||||
package org.thingsboard.server.transport.http;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParseException;
|
||||
import com.google.gson.JsonParser;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
@ -31,6 +32,7 @@ import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
@ -434,7 +436,7 @@ public class DeviceApiController implements TbTransportService {
|
||||
return responseWriter;
|
||||
}
|
||||
|
||||
private static class DeviceAuthCallback implements TransportServiceCallback<ValidateDeviceCredentialsResponse> {
|
||||
static class DeviceAuthCallback implements TransportServiceCallback<ValidateDeviceCredentialsResponse> {
|
||||
private final TransportContext transportContext;
|
||||
private final DeferredResult<ResponseEntity> responseWriter;
|
||||
private final Consumer<SessionInfoProto> onSuccess;
|
||||
@ -456,8 +458,14 @@ public class DeviceApiController implements TbTransportService {
|
||||
|
||||
@Override
|
||||
public void onError(Throwable e) {
|
||||
String body = null;
|
||||
if (e instanceof HttpMessageNotReadableException || e instanceof JsonParseException) {
|
||||
body = e.getMessage();
|
||||
log.debug("Failed to process request: {}", body);
|
||||
} else {
|
||||
log.warn("Failed to process request", e);
|
||||
responseWriter.setResult(new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR));
|
||||
}
|
||||
responseWriter.setResult(new ResponseEntity<>(body, HttpStatus.INTERNAL_SERVER_ERROR));
|
||||
}
|
||||
}
|
||||
|
||||
@ -475,8 +483,14 @@ public class DeviceApiController implements TbTransportService {
|
||||
|
||||
@Override
|
||||
public void onError(Throwable e) {
|
||||
String body = null;
|
||||
if (e instanceof HttpMessageNotReadableException || e instanceof JsonParseException) {
|
||||
body = e.getMessage();
|
||||
log.debug("Failed to process request: {}", body);
|
||||
} else {
|
||||
log.warn("Failed to process request", e);
|
||||
responseWriter.setResult(new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR));
|
||||
}
|
||||
responseWriter.setResult(new ResponseEntity<>(body, HttpStatus.INTERNAL_SERVER_ERROR));
|
||||
}
|
||||
}
|
||||
|
||||
@ -516,8 +530,14 @@ public class DeviceApiController implements TbTransportService {
|
||||
|
||||
@Override
|
||||
public void onError(Throwable e) {
|
||||
String body = null;
|
||||
if (e instanceof HttpMessageNotReadableException || e instanceof JsonParseException) {
|
||||
body = e.getMessage();
|
||||
log.debug("Failed to process request: {}", body);
|
||||
} else {
|
||||
log.warn("Failed to process request", e);
|
||||
responseWriter.setResult(new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR));
|
||||
}
|
||||
responseWriter.setResult(new ResponseEntity<>(body, HttpStatus.INTERNAL_SERVER_ERROR));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -0,0 +1,35 @@
|
||||
package org.thingsboard.server.transport.http;
|
||||
|
||||
import com.google.gson.JsonParseException;
|
||||
import com.google.gson.JsonSyntaxException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||
import org.springframework.web.context.request.async.DeferredResult;
|
||||
import org.thingsboard.server.common.transport.TransportContext;
|
||||
import org.thingsboard.server.gen.transport.TransportProtos;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class DeviceApiControllerTest {
|
||||
|
||||
@Test
|
||||
void callbackOnErrorTest() {
|
||||
TransportContext transportContext = Mockito.mock(TransportContext.class);
|
||||
DeferredResult<ResponseEntity> responseWriter = Mockito.mock(DeferredResult.class);
|
||||
Consumer<TransportProtos.SessionInfoProto> onSuccess = x -> {};
|
||||
var callback = new DeviceApiController.DeviceAuthCallback(transportContext, responseWriter, onSuccess);
|
||||
|
||||
callback.onError(new HttpMessageNotReadableException("JSON incorect syntax"));
|
||||
|
||||
callback.onError(new JsonParseException("Json ; expected"));
|
||||
|
||||
callback.onError(new IOException("not found"));
|
||||
|
||||
callback.onError(new RuntimeException("oops it is run time error"));
|
||||
}
|
||||
}
|
||||
14
common/transport/http/src/test/resources/logback-test.xml
Normal file
14
common/transport/http/src/test/resources/logback-test.xml
Normal file
@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
|
||||
<configuration>
|
||||
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{ISO8601} [%thread] %-5level %logger{36} - %msg%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="TRACE">
|
||||
<appender-ref ref="console"/>
|
||||
</root>
|
||||
|
||||
</configuration>
|
||||
Loading…
x
Reference in New Issue
Block a user