Merge pull request #13737 from ShvaykaD/http-api-error-handling
Handle MethodArgumentTypeMismatchException as 400 Bad Request
This commit is contained in:
		
						commit
						47c0103105
					
				@ -37,6 +37,7 @@ import org.springframework.web.bind.MethodArgumentNotValidException;
 | 
			
		||||
import org.springframework.web.bind.annotation.ExceptionHandler;
 | 
			
		||||
import org.springframework.web.context.request.async.AsyncRequestTimeoutException;
 | 
			
		||||
import org.springframework.web.context.request.async.DeferredResult;
 | 
			
		||||
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
 | 
			
		||||
import org.thingsboard.common.util.DonAsynchron;
 | 
			
		||||
import org.thingsboard.common.util.JacksonUtil;
 | 
			
		||||
import org.thingsboard.server.cluster.TbClusterService;
 | 
			
		||||
@ -458,6 +459,8 @@ public abstract class BaseController {
 | 
			
		||||
            return new ThingsboardException(exception, ThingsboardErrorCode.DATABASE);
 | 
			
		||||
        } else if (exception instanceof EntityVersionMismatchException) {
 | 
			
		||||
            return new ThingsboardException(exception.getMessage(), exception, ThingsboardErrorCode.VERSION_CONFLICT);
 | 
			
		||||
        } else if (exception instanceof MethodArgumentTypeMismatchException) {
 | 
			
		||||
            return new ThingsboardException(exception.getMessage(), exception, ThingsboardErrorCode.BAD_REQUEST_PARAMS);
 | 
			
		||||
        }
 | 
			
		||||
        return new ThingsboardException(exception.getMessage(), exception, ThingsboardErrorCode.GENERAL);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -19,6 +19,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
 | 
			
		||||
import org.junit.Assert;
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
import org.springframework.test.context.TestPropertySource;
 | 
			
		||||
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
 | 
			
		||||
import org.thingsboard.server.common.data.Device;
 | 
			
		||||
import org.thingsboard.server.common.data.SaveDeviceWithCredentialsRequest;
 | 
			
		||||
import org.thingsboard.server.common.data.kv.BasicTsKvEntry;
 | 
			
		||||
@ -33,6 +34,7 @@ import org.thingsboard.server.dao.service.DaoSqlTest;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.concurrent.TimeUnit;
 | 
			
		||||
 | 
			
		||||
import static org.assertj.core.api.Assertions.assertThat;
 | 
			
		||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
 | 
			
		||||
import static org.thingsboard.server.common.data.query.EntityKeyType.TIME_SERIES;
 | 
			
		||||
 | 
			
		||||
@ -208,6 +210,15 @@ public class TelemetryControllerTest extends AbstractControllerTest {
 | 
			
		||||
        doPostAsync("/api/plugins/telemetry/DEVICE/" + device.getId() + "/timeseries/smth", invalidRequestBody, String.class, status().isBadRequest());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testBadRequestReturnedWhenMethodArgumentTypeMismatch() throws Exception {
 | 
			
		||||
        loginTenantAdmin();
 | 
			
		||||
        String content = "{\"key\": \"value\"}";
 | 
			
		||||
        doPost("/api/plugins/telemetry/DEVICE/20b559f5-849f-4361-b4f6-b6d0b76687e9/INVALID_SCOPE", content, (String) null)
 | 
			
		||||
                .andExpect(status().isBadRequest())
 | 
			
		||||
                .andExpect(result -> assertThat(result.getResolvedException()).isInstanceOf(MethodArgumentTypeMismatchException.class));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testEmptyKeyIsProhibited() throws Exception {
 | 
			
		||||
        loginTenantAdmin();
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user