Browse Source

Bad request exception

wfansh 6 tháng trước cách đây
mục cha
commit
7fcca4b223

+ 27 - 0
src/main/java/com/wechi/adweb/bridge/exception/BadRequestException.java

@@ -0,0 +1,27 @@
+package com.wechi.adweb.bridge.exception;
+
+import com.wechi.adweb.bridge.common.OpenAPIRequest;
+
+import lombok.Data;
+
+/**
+ * @author wfansh
+ */
+@Data
+public class BadRequestException extends Exception {
+
+    private OpenAPIRequest apiRequest;
+
+    public BadRequestException() {
+        super();
+    }
+
+    public BadRequestException(OpenAPIRequest<?> apiRequest) {
+        this.apiRequest = apiRequest;
+    }
+
+    public BadRequestException(String message, OpenAPIRequest<?> apiRequest) {
+        super(message);
+        this.apiRequest = apiRequest;
+    }
+}

+ 54 - 0
src/main/java/com/wechi/adweb/bridge/exception/ExceptionAdvice.java

@@ -0,0 +1,54 @@
+package com.wechi.adweb.bridge.exception;
+
+import com.wechi.adweb.bridge.common.APIStatus;
+import com.wechi.adweb.bridge.common.OpenAPIResponse;
+import com.wechi.adweb.bridge.google.analytics.dto.report.data.GAReportDataDTO;
+
+import lombok.extern.slf4j.Slf4j;
+
+import org.springframework.http.HttpStatus;
+import org.springframework.web.bind.annotation.ControllerAdvice;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.ResponseBody;
+import org.springframework.web.bind.annotation.ResponseStatus;
+
+import java.util.List;
+import java.util.Objects;
+
+/**
+ * @author wfansh
+ */
+@Slf4j
+@ControllerAdvice
+public class ExceptionAdvice {
+
+    /**
+     * @return 400 BAD_REQUEST
+     */
+    @ResponseBody
+    @ResponseStatus(HttpStatus.BAD_REQUEST)
+    @ExceptionHandler({DataException.class})
+    public OpenAPIResponse<?> handleBadRequestException(BadRequestException e) {
+        log.error(Objects.toString(e.getApiRequest()), e);
+
+        return OpenAPIResponse.<List<? extends GAReportDataDTO>>builder()
+                .status(APIStatus.BAD_REQUEST)
+                .message("Invalid request parameters")
+                .build();
+    }
+
+    /**
+     * @return 500 INTERNAL_SERVER_ERROR
+     */
+    @ResponseBody
+    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
+    @ExceptionHandler({DataException.class})
+    public OpenAPIResponse<?> handleDataException(DataException e) {
+        log.error(e.getMessage(), e);
+
+        return OpenAPIResponse.builder()
+                .status(APIStatus.INTERNAL_SERVER_ERROR)
+                .message(e.getMessage())
+                .build();
+    }
+}

+ 4 - 5
src/main/java/com/wechi/adweb/bridge/google/analytics/controller/GAController.java

@@ -5,6 +5,7 @@ import com.wechi.adweb.bridge.common.APIStatus;
 import com.wechi.adweb.bridge.common.BaseController;
 import com.wechi.adweb.bridge.common.OpenAPIRequest;
 import com.wechi.adweb.bridge.common.OpenAPIResponse;
+import com.wechi.adweb.bridge.exception.BadRequestException;
 import com.wechi.adweb.bridge.exception.DataException;
 import com.wechi.adweb.bridge.google.analytics.dto.report.GAReportRequestDTO;
 import com.wechi.adweb.bridge.google.analytics.dto.report.ReportType;
@@ -36,7 +37,8 @@ public class GAController extends BaseController {
 
     @RequestMapping("/report")
     public OpenAPIResponse<List<? extends GAReportDataDTO>> getGAReport(
-            @RequestBody OpenAPIRequest<GAReportRequestDTO> apiRequest) throws DataException {
+            @RequestBody OpenAPIRequest<GAReportRequestDTO> apiRequest)
+            throws BadRequestException, DataException {
         long start = System.currentTimeMillis();
         log.info("****** getGAReport() ****** apiRequest = {}", gson.toJson(apiRequest));
         GAReportRequestDTO reportRequest = apiRequest.getData();
@@ -50,10 +52,7 @@ public class GAController extends BaseController {
                 || (ReportType.ADWEB_CUSTOM_REPORT.equals(reportRequest.getReportType())
                         && CollectionUtils.isEmpty(reportRequest.getMetrics())
                         && CollectionUtils.isEmpty(reportRequest.getDimensions()))) {
-            return OpenAPIResponse.<List<? extends GAReportDataDTO>>builder()
-                    .status(APIStatus.BAD_REQUEST)
-                    .message("Invalid request parameters")
-                    .build();
+            throw new BadRequestException(apiRequest);
         }
 
         // 2. Executes API requests.