Add HaloException(s)

pull/98/head
johnniang 2019-02-19 21:23:50 +08:00
parent 90aa7c1077
commit df26777ddc
6 changed files with 160 additions and 0 deletions

View File

@ -0,0 +1,18 @@
package cc.ryanc.halo.exception;
/**
* Exception caused by entity existence already.
*
* @author johnniang
*/
public class AlreadyExistsException extends BadRequestException {
public AlreadyExistsException(String message) {
super(message);
}
public AlreadyExistsException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@ -0,0 +1,24 @@
package cc.ryanc.halo.exception;
import org.springframework.http.HttpStatus;
/**
* Exception caused by bad request.
*
* @author johnniang
*/
public class BadRequestException extends HaloException {
public BadRequestException(String message) {
super(message);
}
public BadRequestException(String message, Throwable cause) {
super(message, cause);
}
@Override
public HttpStatus getStatus() {
return HttpStatus.BAD_REQUEST;
}
}

View File

@ -0,0 +1,24 @@
package cc.ryanc.halo.exception;
import org.springframework.http.HttpStatus;
/**
* Exception caused by accessing forbidden resources.
*
* @author johnniang
*/
public class ForbiddenException extends HaloException {
public ForbiddenException(String message) {
super(message);
}
public ForbiddenException(String message, Throwable cause) {
super(message, cause);
}
@Override
public HttpStatus getStatus() {
return HttpStatus.FORBIDDEN;
}
}

View File

@ -0,0 +1,46 @@
package cc.ryanc.halo.exception;
import org.springframework.http.HttpStatus;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
/**
* Base exception of the project.
*
* @author johnniang
*/
public abstract class HaloException extends RuntimeException {
/**
* Error errorData.
*/
private Object errorData;
public HaloException(String message) {
super(message);
}
public HaloException(String message, Throwable cause) {
super(message, cause);
}
@NonNull
public abstract HttpStatus getStatus();
@Nullable
public Object getErrorData() {
return errorData;
}
/**
* Sets error errorData.
*
* @param errorData error data
* @return current exception.
*/
@NonNull
public HaloException setErrorData(@Nullable Object errorData) {
this.errorData = errorData;
return this;
}
}

View File

@ -0,0 +1,24 @@
package cc.ryanc.halo.exception;
import org.springframework.http.HttpStatus;
/**
* Exception of entity not found.
*
* @author johnniang
*/
public class NotFoundException extends HaloException {
public NotFoundException(String message) {
super(message);
}
public NotFoundException(String message, Throwable cause) {
super(message, cause);
}
@Override
public HttpStatus getStatus() {
return HttpStatus.NOT_FOUND;
}
}

View File

@ -0,0 +1,24 @@
package cc.ryanc.halo.exception;
import org.springframework.http.HttpStatus;
/**
* Exception caused by service.
*
* @author johnniang
*/
public class ServiceException extends HaloException {
public ServiceException(String message) {
super(message);
}
public ServiceException(String message, Throwable cause) {
super(message, cause);
}
@Override
public HttpStatus getStatus() {
return HttpStatus.INTERNAL_SERVER_ERROR;
}
}