Error handling

This commit is contained in:
crusader 2017-08-15 12:38:21 +09:00
parent bacc2da529
commit 4d00504612
11 changed files with 84 additions and 22 deletions

View File

@ -1,10 +1,21 @@
package com.loafle.overflow.commons.exception; package com.loafle.overflow.commons.exception;
import io.grpc.Status; import io.grpc.Status;
import io.grpc.StatusException;
public class OverflowException extends StatusException { public class OverflowException extends Exception {
public OverflowException(Status status) { protected Status.Code code;
super(status);
public OverflowException(Status.Code code) {
super();
this.code = code;
}
public OverflowException(Status.Code code, String message) {
super(message);
this.code = code;
}
public Status.Code getCode() {
return code;
} }
} }

View File

@ -1,10 +1,11 @@
package com.loafle.overflow.commons.exception; package com.loafle.overflow.commons.exception;
import io.grpc.Status; public class OverflowRuntimeException extends RuntimeException {
import io.grpc.StatusRuntimeException; public OverflowRuntimeException() {
super();
}
public class OverflowRuntimeException extends StatusRuntimeException { public OverflowRuntimeException(String message) {
public OverflowRuntimeException(Class<? extends OverflowRuntimeException> clazz) { super(message);
super(Status.fromCode(Status.Code.UNKNOWN).withDescription(clazz.getSimpleName()));
} }
} }

View File

@ -4,6 +4,10 @@ import com.loafle.overflow.commons.exception.OverflowRuntimeException;
public class EmailNotConfirmedException extends OverflowRuntimeException { public class EmailNotConfirmedException extends OverflowRuntimeException {
public EmailNotConfirmedException() { public EmailNotConfirmedException() {
super(EmailNotConfirmedException.class); super();
}
public EmailNotConfirmedException(String message) {
super(message);
} }
} }

View File

@ -4,6 +4,10 @@ import com.loafle.overflow.commons.exception.OverflowRuntimeException;
public class SignInIdNotExistException extends OverflowRuntimeException { public class SignInIdNotExistException extends OverflowRuntimeException {
public SignInIdNotExistException() { public SignInIdNotExistException() {
super(SignInIdNotExistException.class); super();
}
public SignInIdNotExistException(String message) {
super(message);
} }
} }

View File

@ -4,6 +4,10 @@ import com.loafle.overflow.commons.exception.OverflowRuntimeException;
public class SignInPwNotMatchException extends OverflowRuntimeException { public class SignInPwNotMatchException extends OverflowRuntimeException {
public SignInPwNotMatchException() { public SignInPwNotMatchException() {
super(SignInPwNotMatchException.class); super();
}
public SignInPwNotMatchException(String message) {
super(message);
} }
} }

View File

@ -8,9 +8,9 @@ import com.loafle.overflow.proxy.exception.InvalidParameterException;
import com.loafle.overflow.proxy.exception.InvalidRequestException; import com.loafle.overflow.proxy.exception.InvalidRequestException;
import com.loafle.overflow.proxy.exception.NoSuchMethodException; import com.loafle.overflow.proxy.exception.NoSuchMethodException;
import com.loafle.overflow.proxy.exception.NoSuchServiceException; import com.loafle.overflow.proxy.exception.NoSuchServiceException;
import org.codehaus.jackson.JsonGenerationException; import io.grpc.Status;
import io.grpc.StatusRuntimeException;
import org.codehaus.jackson.map.DeserializationConfig; import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.JavaType; import org.codehaus.jackson.type.JavaType;
import org.springframework.aop.support.AopUtils; import org.springframework.aop.support.AopUtils;
@ -199,4 +199,5 @@ public class ServiceInvoker {
return jsonInString; return jsonInString;
} }
} }

View File

@ -3,8 +3,12 @@ package com.loafle.overflow.proxy;
import com.loafle.overflow.api.OverflowApiServerGrpc; import com.loafle.overflow.api.OverflowApiServerGrpc;
import com.loafle.overflow.api.ServerInput; import com.loafle.overflow.api.ServerInput;
import com.loafle.overflow.api.ServerOutput; import com.loafle.overflow.api.ServerOutput;
import com.loafle.overflow.commons.exception.OverflowException;
import com.loafle.overflow.commons.exception.OverflowRuntimeException;
import io.grpc.ServerBuilder; import io.grpc.ServerBuilder;
import io.grpc.Status;
import io.grpc.StatusException; import io.grpc.StatusException;
import io.grpc.StatusRuntimeException;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@ -73,15 +77,28 @@ public class ServiceProxy {
.build(); .build();
responseObserver.onNext(reply); responseObserver.onNext(reply);
responseObserver.onCompleted(); responseObserver.onCompleted();
} catch (StatusException e) { } catch (OverflowException e) {
logger.warning(e.toString()); logger.warning(e.toString());
responseObserver.onError(e); responseObserver.onError(convertException(e));
} catch (Throwable e) { } catch (OverflowRuntimeException e) {
responseObserver.onError(e); logger.warning(e.toString());
responseObserver.onError(convertException(e));
} }
} }
protected StatusRuntimeException convertException(OverflowRuntimeException e) {
String message = String.format("%s|%s", e.getClass().getSimpleName(), e.getMessage());
Status status = Status.fromCode(Status.Code.INTERNAL).withDescription(message);
return new StatusRuntimeException(status);
}
protected StatusException convertException(OverflowException e) {
String message = String.format("%s|%s", e.getClass().getSimpleName(), e.getMessage());
Status status = Status.fromCode(e.getCode()).withDescription(message);
return new StatusException(status);
}
} }
} }

View File

@ -4,7 +4,12 @@ import com.loafle.overflow.commons.exception.OverflowException;
import io.grpc.Status; import io.grpc.Status;
public class InvalidParameterException extends OverflowException { public class InvalidParameterException extends OverflowException {
private static final Status.Code code = Status.Code.INVALID_ARGUMENT;
public InvalidParameterException() { public InvalidParameterException() {
super(Status.fromCode(Status.Code.INVALID_ARGUMENT).withDescription(InvalidParameterException.class.getSimpleName())); super(code);
}
public InvalidParameterException(String message) {
super(code, message);
} }
} }

View File

@ -4,7 +4,12 @@ import com.loafle.overflow.commons.exception.OverflowException;
import io.grpc.Status; import io.grpc.Status;
public class InvalidRequestException extends OverflowException { public class InvalidRequestException extends OverflowException {
private static final Status.Code code = Status.Code.UNKNOWN;
public InvalidRequestException() { public InvalidRequestException() {
super(Status.fromCode(Status.Code.INTERNAL).withDescription(InvalidRequestException.class.getSimpleName())); super(code);
}
public InvalidRequestException(String message) {
super(code, message);
} }
} }

View File

@ -4,7 +4,12 @@ import com.loafle.overflow.commons.exception.OverflowException;
import io.grpc.Status; import io.grpc.Status;
public class NoSuchMethodException extends OverflowException { public class NoSuchMethodException extends OverflowException {
private static final Status.Code code = Status.Code.UNIMPLEMENTED;
public NoSuchMethodException() { public NoSuchMethodException() {
super(Status.fromCode(Status.Code.UNIMPLEMENTED).withDescription(NoSuchMethodException.class.getSimpleName())); super(code);
}
public NoSuchMethodException(String message) {
super(code, message);
} }
} }

View File

@ -4,7 +4,12 @@ import com.loafle.overflow.commons.exception.OverflowException;
import io.grpc.Status; import io.grpc.Status;
public class NoSuchServiceException extends OverflowException { public class NoSuchServiceException extends OverflowException {
private static final Status.Code code = Status.Code.UNIMPLEMENTED;
public NoSuchServiceException() { public NoSuchServiceException() {
super(Status.fromCode(Status.Code.UNAVAILABLE).withDescription(NoSuchServiceException.class.getSimpleName())); super(code);
}
public NoSuchServiceException(String message) {
super(code, message);
} }
} }