自定义 HttpStatus 代码到列不能为空
Customizing HttpStatus code to column cannot be null
当我有 java.sql.SQLIntegrityConstraintViolationException: Column 'xpto' cannot be null
时 return 在休息时 Api 的最佳 HttpStatus 代码是什么?
我有其他类型的异常处理程序,但没有空字段:
@ExceptionHandler({DataIntegrityViolationException.class})
protected ResponseEntity<Object> handleDataIntegrityViolation(DataIntegrityViolationException ex, WebRequest request) {
Throwable cause = ex.getRootCause();
if (cause instanceof SQLIntegrityConstraintViolationException) {
SQLIntegrityConstraintViolationException consEx = (SQLIntegrityConstraintViolationException)cause;
String message = "";
String constraint = "";
HttpStatus httpStatus = null;
if (consEx.getMessage().contains("UNIQUE")) {
message = "Cannot enter the same record twice";
constraint = "DUPLICATED_RECORD";
httpStatus = HttpStatus.CONFLICT;
} else if (consEx.getMessage().contains("foreign key constraint")) {
message = "Record still have reference from other table";
constraint = "USED_RECORD";
httpStatus = HttpStatus.UNPROCESSABLE_ENTITY;
}
return this.buildResponseEntity(new ApiError(httpStatus, message, consEx.getMessage(), constraint));
} else {
return ex.getCause() instanceof org.hibernate.exception.ConstraintViolationException ? this.buildResponseEntity(new ApiError(HttpStatus.CONFLICT, "Database error", ex)) : this.buildResponseEntity(new ApiError(HttpStatus.INTERNAL_SERVER_ERROR, ex));
}
}
我已经对 column cannot be null
添加了处理并使用 400 - BAD_REQUEST 作为 http 状态代码:
@ExceptionHandler(DataIntegrityViolationException.class)
protected ResponseEntity<Object> handleDataIntegrityViolation(DataIntegrityViolationException ex,
WebRequest request) {
Throwable cause = ex.getRootCause();
if (cause instanceof SQLIntegrityConstraintViolationException) {
SQLIntegrityConstraintViolationException consEx = (SQLIntegrityConstraintViolationException) cause;
String message = "";
String constraint = "";
HttpStatus httpStatus = null;
if (consEx.getMessage().contains("UNIQUE")) {
message = "Cannot enter the same record twice";
constraint = "DUPLICATED_RECORD";
httpStatus = HttpStatus.CONFLICT;
} else if (consEx.getMessage().contains("foreign key constraint")) {
message = "Record still have reference from other table";
constraint = "USED_RECORD";
httpStatus = HttpStatus.UNPROCESSABLE_ENTITY;
} else if (consEx.getMessage().contains("cannot be null")){
message = "Record cannot be null";
constraint = "NULL_RECORD";
httpStatus = BAD_REQUEST;
}
return buildResponseEntity(new ApiError(httpStatus, message, consEx.getMessage(), constraint));
} else if (ex.getCause() instanceof ConstraintViolationException) {
return buildResponseEntity(new ApiError(HttpStatus.CONFLICT, "Database error", ex));
}
return buildResponseEntity(new ApiError(HttpStatus.INTERNAL_SERVER_ERROR, ex));
}
当我有 java.sql.SQLIntegrityConstraintViolationException: Column 'xpto' cannot be null
时 return 在休息时 Api 的最佳 HttpStatus 代码是什么?
我有其他类型的异常处理程序,但没有空字段:
@ExceptionHandler({DataIntegrityViolationException.class})
protected ResponseEntity<Object> handleDataIntegrityViolation(DataIntegrityViolationException ex, WebRequest request) {
Throwable cause = ex.getRootCause();
if (cause instanceof SQLIntegrityConstraintViolationException) {
SQLIntegrityConstraintViolationException consEx = (SQLIntegrityConstraintViolationException)cause;
String message = "";
String constraint = "";
HttpStatus httpStatus = null;
if (consEx.getMessage().contains("UNIQUE")) {
message = "Cannot enter the same record twice";
constraint = "DUPLICATED_RECORD";
httpStatus = HttpStatus.CONFLICT;
} else if (consEx.getMessage().contains("foreign key constraint")) {
message = "Record still have reference from other table";
constraint = "USED_RECORD";
httpStatus = HttpStatus.UNPROCESSABLE_ENTITY;
}
return this.buildResponseEntity(new ApiError(httpStatus, message, consEx.getMessage(), constraint));
} else {
return ex.getCause() instanceof org.hibernate.exception.ConstraintViolationException ? this.buildResponseEntity(new ApiError(HttpStatus.CONFLICT, "Database error", ex)) : this.buildResponseEntity(new ApiError(HttpStatus.INTERNAL_SERVER_ERROR, ex));
}
}
我已经对 column cannot be null
添加了处理并使用 400 - BAD_REQUEST 作为 http 状态代码:
@ExceptionHandler(DataIntegrityViolationException.class)
protected ResponseEntity<Object> handleDataIntegrityViolation(DataIntegrityViolationException ex,
WebRequest request) {
Throwable cause = ex.getRootCause();
if (cause instanceof SQLIntegrityConstraintViolationException) {
SQLIntegrityConstraintViolationException consEx = (SQLIntegrityConstraintViolationException) cause;
String message = "";
String constraint = "";
HttpStatus httpStatus = null;
if (consEx.getMessage().contains("UNIQUE")) {
message = "Cannot enter the same record twice";
constraint = "DUPLICATED_RECORD";
httpStatus = HttpStatus.CONFLICT;
} else if (consEx.getMessage().contains("foreign key constraint")) {
message = "Record still have reference from other table";
constraint = "USED_RECORD";
httpStatus = HttpStatus.UNPROCESSABLE_ENTITY;
} else if (consEx.getMessage().contains("cannot be null")){
message = "Record cannot be null";
constraint = "NULL_RECORD";
httpStatus = BAD_REQUEST;
}
return buildResponseEntity(new ApiError(httpStatus, message, consEx.getMessage(), constraint));
} else if (ex.getCause() instanceof ConstraintViolationException) {
return buildResponseEntity(new ApiError(HttpStatus.CONFLICT, "Database error", ex));
}
return buildResponseEntity(new ApiError(HttpStatus.INTERNAL_SERVER_ERROR, ex));
}