Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.doubleo.hospitalservice.domain.area.repository.AreaRepository;
import com.doubleo.hospitalservice.domain.building.domain.Building;
import com.doubleo.hospitalservice.domain.building.repository.BuildingRepository;
import com.doubleo.hospitalservice.global.exception.CommonException;
import com.doubleo.hospitalservice.global.exception.GrpcExceptionUtil;
import com.doubleo.hospitalservice.global.exception.errorcode.AreaErrorCode;
import com.doubleo.hospitalservice.global.exception.errorcode.BuildingErrorCode;
import io.grpc.Status;
Expand Down Expand Up @@ -48,7 +48,8 @@ public void getAreaById(AreaIdRequest request, StreamObserver<AreaResponse> resp
},
() -> {
responseObserver.onError(
new CommonException(AreaErrorCode.AREA_NOT_FOUND));
GrpcExceptionUtil.toStatusRuntimeException(
AreaErrorCode.AREA_NOT_FOUND));
});
}

Expand Down Expand Up @@ -101,7 +102,8 @@ public void getAreaFullNameByCode(
sb.append(building.get().getBuildingName());
} else {
responseObserver.onError(
new CommonException(BuildingErrorCode.BUILDING_NOT_FOUND));
GrpcExceptionUtil.toStatusRuntimeException(
BuildingErrorCode.BUILDING_NOT_FOUND));
return;
}
} else if (i == 1) {
Expand All @@ -118,7 +120,9 @@ public void getAreaFullNameByCode(
if (area.isPresent()) {
sb.append(area.get().getAreaName());
} else {
responseObserver.onError(new CommonException(AreaErrorCode.AREA_NOT_FOUND));
responseObserver.onError(
GrpcExceptionUtil.toStatusRuntimeException(
AreaErrorCode.AREA_NOT_FOUND));
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.doubleo.hospitalservice.domain.building.grpc.server;

import com.doubleo.hospitalservice.domain.building.repository.BuildingRepository;
import com.doubleo.hospitalservice.global.exception.CommonException;
import com.doubleo.hospitalservice.global.exception.GrpcExceptionUtil;
import com.doubleo.hospitalservice.global.exception.errorcode.BuildingErrorCode;
import io.grpc.stub.StreamObserver;
import net.devh.boot.grpc.server.service.GrpcService;
Expand Down Expand Up @@ -33,7 +33,8 @@ public void getBuildingById(
},
() -> {
responseObserver.onError(
new CommonException(BuildingErrorCode.BUILDING_NOT_FOUND));
GrpcExceptionUtil.toStatusRuntimeException(
BuildingErrorCode.BUILDING_NOT_FOUND));
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.doubleo.hospitalservice.global.exception;

import com.doubleo.hospitalservice.global.exception.errorcode.BaseErrorCode;
import com.doubleo.hospitalservice.global.exception.errorcode.ErrorCodeRegistry;
import com.doubleo.hospitalservice.global.exception.errorcode.GrpcErrorCode;
import io.grpc.Metadata;
import io.grpc.Status;
import io.grpc.StatusRuntimeException;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class GrpcExceptionUtil {

private static final Metadata.Key<String> CODE_KEY =
Metadata.Key.of("code", Metadata.ASCII_STRING_MARSHALLER);
private static final Metadata.Key<String> CLASS_KEY =
Metadata.Key.of("class", Metadata.ASCII_STRING_MARSHALLER);
private static final Metadata.Key<String> MESSAGE_KEY =
Metadata.Key.of("message", Metadata.ASCII_STRING_MARSHALLER);

public static StatusRuntimeException toStatusRuntimeException(BaseErrorCode errorCode) {
Metadata metadata = new Metadata();
metadata.put(CODE_KEY, errorCode.errorClassName());
metadata.put(CLASS_KEY, errorCode.getClass().getSimpleName());
metadata.put(MESSAGE_KEY, errorCode.getMessage());

return Status.fromCodeValue(errorCode.getHttpStatus().value())
.withDescription(errorCode.getMessage())
.asRuntimeException(metadata);
}

public static CommonException fromStatusRuntimeException(StatusRuntimeException e) {
Metadata metadata = Status.trailersFromThrowable(e);
if (metadata != null) {
String code = metadata.get(CODE_KEY);
String className = metadata.get(CLASS_KEY);
String message = metadata.get(MESSAGE_KEY);

log.warn("gRPC Error - code: {}, message: {}", code, message);
return new CommonException(ErrorCodeRegistry.resolve(className, code));
}
return new CommonException(GrpcErrorCode.GRPC_SERVER_RESPONSE_FAILED);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.doubleo.hospitalservice.global.exception.errorcode;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

public class ErrorCodeRegistry {
private static final Map<String, Function<String, ? extends BaseErrorCode>> registry =
new HashMap<>();

static {
registry.put("GlobalErrorCode", GlobalErrorCode::valueOf);
registry.put("GrpcErrorCode", GrpcErrorCode::valueOf);
registry.put("TenantErrorCode", TenantErrorCode::valueOf);
registry.put("BuildingErrorCode", BuildingErrorCode::valueOf);
registry.put("AreaErrorCode", AreaErrorCode::valueOf);
registry.put("HospitalErrorCode", HospitalErrorCode::valueOf);
registry.put("HospitalPolicyErrorCode", HospitalPolicyErrorCode::valueOf);
}

public static BaseErrorCode resolve(String className, String code) {
Function<String, ? extends BaseErrorCode> parser = registry.get(className);
if (parser == null) {
throw new IllegalArgumentException("Unknown class " + className);
}
try {
return parser.apply(code);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException(
"Invalid error code " + code + " for class " + className, e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.doubleo.hospitalservice.global.exception.errorcode;

import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.http.HttpStatus;

@Getter
@AllArgsConstructor
public enum GrpcErrorCode implements BaseErrorCode {
GRPC_SERVER_RESPONSE_FAILED(HttpStatus.NOT_FOUND, "gRPC 호출에 실패했습니다."),
;

private final HttpStatus httpStatus;
private final String message;

@Override
public String errorClassName() {
return this.name();
}
}