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 20, 2024
1 parent bd9fe40 commit 99bd9a1
Show file tree
Hide file tree
Showing 10 changed files with 269 additions and 56 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 All @@ -21,39 +25,58 @@
@Slf4j
public class MetricDService {

public static final String METRICD_URI = "/metricD";

private final MetricDDao metricDDao;

public MetricDService(MetricDDao metricDDao) {
this.metricDDao = metricDDao;
}

@GetMapping(value = "/ping", produces = MediaType.APPLICATION_JSON_VALUE)
public String ping(){
public String ping() {
return "hello";
}

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

@PutMapping(value = "/metricD", consumes = MediaType.APPLICATION_JSON_VALUE,
@PutMapping(value = METRICD_URI, 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,
@PostMapping(value = METRICD_URI, consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
public MetricD updateMetricD(MetricD metricD){
@ResponseStatus(HttpStatus.ACCEPTED)
public ResponseEntity<BaseEntity> updateMetricD(MetricD metricD) {
boolean ret = metricDDao.updateById(metricD);
return ret ? metricD : null;
return ret ? new ResponseEntity<>(metricD, HttpStatus.ACCEPTED) : new ResponseEntity<>(
GriffinErr.dbUpdateError.buildErrorEntity(),
HttpStatus.INTERNAL_SERVER_ERROR);
}

@DeleteMapping(value = "/metricD/{id}")
public boolean deleteMetricD(@PathVariable @NonNull String id){
return metricDDao.deleteById(id);
@DeleteMapping(value = METRICD_URI + "/{id}")
@ResponseStatus(HttpStatus.OK)
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);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package org.apache.griffin.metric.service;

import lombok.extern.slf4j.Slf4j;
import org.apache.griffin.metric.dao.MetricTagDDao;
import org.apache.griffin.metric.dao.TagAttachmentDao;
import org.apache.griffin.metric.entity.BaseEntity;
import org.apache.griffin.metric.entity.MetricTagD;
import org.apache.griffin.metric.entity.TagAttachment;
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.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class MetricTagService {

public static final String METRIC_TAG_D = "/metricTagD";

public static final String TAGS = "/tags";

private final MetricTagDDao metricTagDDao;

private final TagAttachmentDao tagAttachmentDao;

public MetricTagService(MetricTagDDao metricTagDDao, TagAttachmentDao tagAttachmentDao) {
this.metricTagDDao = metricTagDDao;
this.tagAttachmentDao = tagAttachmentDao;
}

@PutMapping(value = METRIC_TAG_D,consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.CREATED)
public ResponseEntity<BaseEntity> createMetricTagD(@RequestBody MetricTagD metricTagD) {
try {
int id = metricTagDDao.addMetricTagD(metricTagD);
if (id != 1) {
return new ResponseEntity<>(GriffinErr.dbInsertionError.buildErrorEntity(),
HttpStatus.INTERNAL_SERVER_ERROR);
}
return new ResponseEntity<>(metricTagD, HttpStatus.CREATED);
} catch (GriffinException e) {
return new ResponseEntity<>(e.getError().buildErrorEntity(e.getMessage()),
HttpStatus.INTERNAL_SERVER_ERROR);
}
}

@PostMapping(value = METRIC_TAG_D, consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.ACCEPTED)
public ResponseEntity<BaseEntity> updateMetricTagD(@RequestBody MetricTagD metricTagD) {
boolean ret = metricTagDDao.updateById(metricTagD);
return ret ? new ResponseEntity<>(metricTagD, HttpStatus.ACCEPTED) : new ResponseEntity<>(
GriffinErr.dbUpdateError.buildErrorEntity(),
HttpStatus.INTERNAL_SERVER_ERROR);
}

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

@PutMapping(value = TAGS,consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.CREATED)
public ResponseEntity<BaseEntity> attachTags(@RequestBody TagAttachment tagAttachment) {
try {
int id = tagAttachmentDao.addMetricTags(tagAttachment);
if (id != 1) {
return new ResponseEntity<>(GriffinErr.dbInsertionError.buildErrorEntity(),
HttpStatus.INTERNAL_SERVER_ERROR);
}
return new ResponseEntity<>(tagAttachment, HttpStatus.CREATED);
} catch (GriffinException e) {
return new ResponseEntity<>(e.getError().buildErrorEntity(e.getMessage()),
HttpStatus.INTERNAL_SERVER_ERROR);
}
}

@PostMapping(value = TAGS, consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.ACCEPTED)
public ResponseEntity<BaseEntity> updateMetricD(@RequestBody TagAttachment tagAttachment) {
boolean ret = tagAttachmentDao.updateById(tagAttachment);
return ret ? new ResponseEntity<>(tagAttachment, HttpStatus.ACCEPTED) : new ResponseEntity<>(
GriffinErr.dbUpdateError.buildErrorEntity(),
HttpStatus.INTERNAL_SERVER_ERROR);
}

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

0 comments on commit 99bd9a1

Please sign in to comment.