diff --git a/griffin-metric/src/main/java/org/apache/griffin/metric/exception/GriffinErr.java b/griffin-metric/src/main/java/org/apache/griffin/metric/exception/GriffinErr.java new file mode 100644 index 000000000..744561be4 --- /dev/null +++ b/griffin-metric/src/main/java/org/apache/griffin/metric/exception/GriffinErr.java @@ -0,0 +1,19 @@ +package org.apache.griffin.metric.exception; + +import lombok.Getter; + +public enum GriffinErr { + commonError(100, ""); + + @Getter + private int code; + + @Getter + private String message; + + GriffinErr(int code, String message) { + this.code = code; + this.message = message; + } + +} diff --git a/griffin-metric/src/main/java/org/apache/griffin/metric/exception/GriffinException.java b/griffin-metric/src/main/java/org/apache/griffin/metric/exception/GriffinException.java index 000491235..569149d6c 100644 --- a/griffin-metric/src/main/java/org/apache/griffin/metric/exception/GriffinException.java +++ b/griffin-metric/src/main/java/org/apache/griffin/metric/exception/GriffinException.java @@ -1,15 +1,28 @@ package org.apache.griffin.metric.exception; +import lombok.Getter; + public class GriffinException extends RuntimeException { + @Getter + private final int errorCode; + public GriffinException(String message) { super(message); + this.errorCode = GriffinErr.commonError.getCode(); } public GriffinException(Throwable cause) { super(cause); + this.errorCode = GriffinErr.commonError.getCode(); } public GriffinException(String message, Throwable cause) { super(message, cause); + this.errorCode = GriffinErr.commonError.getCode(); + } + + public GriffinException(String message, int errorCode) { + super(message); + this.errorCode = errorCode; } } diff --git a/griffin-metric/src/main/java/org/apache/griffin/metric/service/MetricDService.java b/griffin-metric/src/main/java/org/apache/griffin/metric/service/MetricDService.java index f1e45a2a2..7c0a8afe6 100644 --- a/griffin-metric/src/main/java/org/apache/griffin/metric/service/MetricDService.java +++ b/griffin-metric/src/main/java/org/apache/griffin/metric/service/MetricDService.java @@ -3,8 +3,10 @@ import lombok.extern.slf4j.Slf4j; import org.apache.griffin.metric.dao.MetricDDao; import org.apache.griffin.metric.entity.MetricD; +import org.apache.griffin.metric.exception.GriffinException; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; import org.springframework.lang.NonNull; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; @@ -40,9 +42,17 @@ public List allMetricDs(){ @PutMapping(value = "/metricD", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseStatus(HttpStatus.CREATED) - public MetricD createMetricD(@RequestBody MetricD metricD){ - int id = metricDDao.addMetricD(metricD); - return metricD; + public ResponseEntity createMetricD(@RequestBody MetricD metricD) { + try { + int id = metricDDao.addMetricD(metricD); + if (id != 1) { + return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR); + } + return new ResponseEntity(metricD, HttpStatus.CREATED); + } catch (GriffinException e) { + int errorCode = e.getErrorCode(); + return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR); + } } @PostMapping(value = "/metricD", consumes = MediaType.APPLICATION_JSON_VALUE,