Skip to content

Commit

Permalink
add the process of exception.
Browse files Browse the repository at this point in the history
  • Loading branch information
toyboxman committed Oct 10, 2024
1 parent bd9fe40 commit 48b58cb
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -40,9 +42,17 @@ public List<MetricD> 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<MetricD> 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,
Expand Down

0 comments on commit 48b58cb

Please sign in to comment.