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 14, 2024
1 parent bd9fe40 commit 25e36bd
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@
import lombok.extern.slf4j.Slf4j;
import org.apache.griffin.metric.dao.mapper.MetricDMapper;
import org.apache.griffin.metric.entity.MetricD;
import org.apache.griffin.metric.exception.GriffinErr;
import org.apache.griffin.metric.exception.GriffinException;
import org.springframework.stereotype.Repository;

@Repository
@Slf4j
public class MetricDDao extends BaseDao<MetricD, MetricDMapper> {

public static final String NOT_BE_NULL = "The metricD argument is illegal.\n" +
public static final String NOT_BE_NULL = "The metricD argument is illegal." +
"Either metricD entity or metric name attribute must not be null.";

public MetricDDao(MetricDMapper metricDMapper) {
Expand All @@ -49,7 +50,7 @@ public int addMetricD(MetricD metricD) {
private void validateEntity(MetricD metricD) {
if (null == metricD || null == metricD.getMetricName()) {
log.error(NOT_BE_NULL);
throw new GriffinException(NOT_BE_NULL);
throw new GriffinException(NOT_BE_NULL, GriffinErr.validationError);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ Licensed to the Apache Software Foundation (ASF) under one
package org.apache.griffin.metric.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

import java.util.Date;
Expand All @@ -28,19 +30,22 @@ Licensed to the Apache Software Foundation (ASF) under one
* A base class in metric function in griffin, which contains timestamp properties of entity creation/update.
*/
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public abstract class BaseEntity implements java.io.Serializable {

private static final long serialVersionUID = 2110740953277261851L;

/**
* creation time
*/
@JsonProperty(value = "creation_time")
@TableField(value = "ctime")
protected Date ctime;

/**
* update time
*/
@JsonProperty(value = "update_time")
@TableField(value = "mtime")
protected Date mtime;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Licensed to the Apache Software Foundation (ASF) under one
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand All @@ -37,6 +38,7 @@ Licensed to the Apache Software Foundation (ASF) under one
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
@JsonInclude(JsonInclude.Include.NON_NULL)
@TableName("t_metric_d")
public class MetricD extends BaseEntity {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.apache.griffin.metric.exception;

import lombok.Getter;

public enum GriffinErr {
commonError(101, "Hit an error without details."),
validationError(102, "Data validation fails due to [%s]"),
// db operation errors
dbInsertionError(301, "Fail to insert a record."),
dbUpdateError(302, "Fail to update a record."),
dbDeletionError(303, "Fail to delete a record."),
;

@Getter
private int code;

@Getter
private String message;

GriffinErr(int code, String message) {
this.code = code;
this.message = message;
}

public GriffinErrorEntity buildErrorEntity() {
return new GriffinErrorEntity(this);
}

public GriffinErrorEntity buildErrorEntity(String message) {
return new GriffinErrorEntity(this, message);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.apache.griffin.metric.exception;

import lombok.Data;
import org.apache.griffin.metric.entity.BaseEntity;

@Data
public class GriffinErrorEntity extends BaseEntity {

private Integer code;

private String message;

public GriffinErrorEntity(GriffinErr err) {
this.code = err.getCode();
this.message = err.getMessage();
}

public GriffinErrorEntity(GriffinErr err, String details) {
this.code = err.getCode();
this.message = String.format(err.getMessage(), details);
}
}
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 GriffinErr error;

public GriffinException(String message) {
super(message);
this.error = GriffinErr.commonError;
}

public GriffinException(Throwable cause) {
super(cause);
this.error = GriffinErr.commonError;
}

public GriffinException(String message, Throwable cause) {
super(message, cause);
this.error = GriffinErr.commonError;
}

public GriffinException(String message, GriffinErr error) {
super(message);
this.error = error;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

import lombok.extern.slf4j.Slf4j;
import org.apache.griffin.metric.dao.MetricDDao;
import org.apache.griffin.metric.entity.BaseEntity;
import org.apache.griffin.metric.entity.MetricD;
import org.apache.griffin.metric.exception.GriffinErr;
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 @@ -33,27 +37,42 @@ public String ping(){
}

@GetMapping(value = "/allMetricD", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK)
public List<MetricD> allMetricDs(){
return metricDDao.queryAll();
}

@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<BaseEntity> createMetricD(@RequestBody MetricD metricD) {
try {
int id = metricDDao.addMetricD(metricD);
if (id != 1) {
return new ResponseEntity<>(GriffinErr.dbInsertionError.buildErrorEntity(),
HttpStatus.INTERNAL_SERVER_ERROR);
}
return new ResponseEntity<>(metricD, HttpStatus.CREATED);
} catch (GriffinException e) {
return new ResponseEntity<>(e.getError().buildErrorEntity(e.getMessage()),
HttpStatus.INTERNAL_SERVER_ERROR);
}
}

@PostMapping(value = "/metricD", consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
public MetricD updateMetricD(MetricD metricD){
public ResponseEntity<BaseEntity> updateMetricD(MetricD metricD){
boolean ret = metricDDao.updateById(metricD);
return ret ? metricD : null;
return ret ? new ResponseEntity<>(metricD, HttpStatus.OK) : new ResponseEntity<>(
GriffinErr.dbUpdateError.buildErrorEntity(),
HttpStatus.INTERNAL_SERVER_ERROR);
}

@DeleteMapping(value = "/metricD/{id}")
public boolean deleteMetricD(@PathVariable @NonNull String id){
return metricDDao.deleteById(id);
public ResponseEntity<BaseEntity> deleteMetricD(@PathVariable @NonNull String id){
boolean ret = metricDDao.deleteById(id);
return ret ? new ResponseEntity<>(HttpStatus.OK) : new ResponseEntity<>(
GriffinErr.dbDeletionError.buildErrorEntity(),
HttpStatus.INTERNAL_SERVER_ERROR);
}
}

0 comments on commit 25e36bd

Please sign in to comment.