반응형
예외처리
//Controller
// ...
@ExceptionHandler(CInvalidRequestParameterException.class)
public ResponseEntity<ErrorResponse> handleInvalidRequestParameterError(CInvalidRequestParameterException e){
logger.error("InvalidRequestParameterError", e);
final ErrorResponse response = ErrorResponse.of(ErrorCode.INVALID_REQUEST_PARAMETER_ERROR);
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
}
@ExceptionHandler(CAuthenticationFailedException.class)
public ResponseEntity<ErrorResponse> handleAuthenticationFailedError(CAuthenticationFailedException e){
logger.error("AuthenticationFailedError", e);
final ErrorResponse response = ErrorResponse.of(ErrorCode.AUTHENTICATION_FAILED_ERROR);
return new ResponseEntity<>(response, HttpStatus.UNAUTHORIZED);
}
@ExceptionHandler(CInvalidRequestIpAddressException.class)
public ResponseEntity<ErrorResponse> handleInvalidRequestIpAddress(CInvalidRequestIpAddressException e){
logger.error("InvalidRequestIpAddress", e);
final ErrorResponse response = ErrorResponse.of(ErrorCode.INVALID_REQUEST_IP_ADDRESS);
return new ResponseEntity<>(response, HttpStatus.PAYMENT_REQUIRED);
}
@ExceptionHandler(CNullPointException.class)
public ResponseEntity<ErrorResponse> handleNullPoint(CNullPointException e){
logger.error("NullPoint", e);
final ErrorResponse response = ErrorResponse.of(ErrorCode.NULL_POINT);
return new ResponseEntity<>(response, HttpStatus.OK);
}
@ExceptionHandler(CInvalidParameterValueException.class)
public ResponseEntity<ErrorResponse> handleInvalidParameterValue(CInvalidParameterValueException e){
logger.error("InvalidParameterValue", e);
final ErrorResponse response = ErrorResponse.of(ErrorCode.INVALID_PAPAMETER_VALUE);
return new ResponseEntity<>(response, HttpStatus.BAD_GATEWAY);
}
@ExceptionHandler(CApplicationException.class)
public ResponseEntity<ErrorResponse> handleApplicationError(CApplicationException e){
logger.error("ApplicationError", e);
final ErrorResponse response = ErrorResponse.of(ErrorCode.APPLICATION_ERROR);
return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
}
@ExceptionHandler(Exception.class)
public ResponseEntity<ErrorResponse> handleException(Exception e) {
logger.error("UnkownError", e);
ErrorResponse response = ErrorResponse.of(ErrorCode.UNKNOWN_ERROR);
HttpStatus httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
if (e instanceof MissingServletRequestParameterException) {
response = ErrorResponse.of(ErrorCode.INVALID_PAPAMETER_VALUE);
httpStatus = HttpStatus.BAD_GATEWAY;
}
return new ResponseEntity<>(response, httpStatus);
}
@ApiOperation(value = "scatter", notes = "scatter 그래프 사용 지표")
@ApiResponses({ @ApiResponse(code = 200, message = "OK"),
@ApiResponse(code = 400, message = "INVALID_REQUEST_PARAMETER_ERROR"),
@ApiResponse(code = 401, message = "AUTHENTICATION_FAILED_ERROR"),
@ApiResponse(code = 402, message = "INVALID_REQUEST_IP_ADDRESS"),
@ApiResponse(code = 404, message = "NULL_POINT"),
@ApiResponse(code = 409, message = "INVALID_PAPAMETER_VALUE"),
@ApiResponse(code = 500, message = "APPLICATION_ERROR"),
@ApiResponse(code = 99, message = "UNKNOWN_ERROR") })
@ApiImplicitParams({
@ApiImplicitParam(name ="dataCode", value = "데이터코드(6자리)", paramType="query"),
@ApiImplicitParam(name ="regionType", value = "지역코드", paramType="query")
})
@GetMapping(value = "/table/cancerData/scatter")
public ResponseEntity<?> scatterGraphData(@RequestParam String dataCode, @RequestParam String regionType)
throws Exception {
HashMap<String, Object> resultMap = new HashMap<String, Object>();
HashMap<String, String> dataCodeMap = new HashMap<String, String>();
dataCodeMap.put("dataCode", dataCode);
dataCodeMap.put("regionType", regionType);
// if(dataCodeMap.get("dataCode") == null || dataCodeMap.get("regionType") == null){
// throw new CInvalidParameterValueException();
// }
// try {
//...
if (headerDataList.isEmpty() || headerInfo.isEmpty() || minmaxYears.isEmpty()
|| tableDataList.isEmpty() || mobileTableDataList.isEmpty()) {
throw new CNullPointException();
}
// } catch(CAuthenticationFailedException e){
// throw new CAuthenticationFailedException(e);
// } catch(CInvalidRequestIpAddressException e){
// throw new CInvalidRequestIpAddressException(e);
// } catch(CNullPointException e){
// throw new CNullPointException(e);
// } catch(CInvalidParameterValueException e){
// throw new CInvalidParameterValueException(e);
// } catch(CApplicationException e){
// throw new CApplicationException(e);
// } catch(Exception e) {
//// final ErrorResponse response = ErrorResponse.of(ErrorCode.UNKNOWN_ERROR);
//// return new ResponseEntity<>(response, HttpStatus.BAD_GATEWAY);
// throw new Exception(e);
// }
return ResponseEntity.ok(resultMap);
}
// ...
// CustomException
package ncdc.cancermonitor.cmmn.exception;
public class CApplicationException extends Exception {
public CApplicationException() {
super();
}
public CApplicationException(Throwable exception) {
super(exception);
}
public CApplicationException(String message, Throwable exception) {
super(message, exception);
}
}
//ErrorResponse.java
package ncdc.cancermonitor.cmmn.exception;
public class ErrorResponse {
private String message;
private int status;
private void setErrorCode(ErrorCode errorCode){
this.message = errorCode.getMessage();
this.status = errorCode.getStatus();
}
private ErrorResponse(ErrorCode errorCode) {
setErrorCode(errorCode);
}
public static ErrorResponse of(ErrorCode errorCode){
return new ErrorResponse(errorCode);
}
public String getMessage() { return message; }
public int getStatus() { return status; }
public enum ErrorCode {
INVALID_REQUEST_PARAMETER_ERROR(400, "INVALID_REQUEST_PARAMETER_ERROR"),
AUTHENTICATION_FAILED_ERROR(401, "AUTHENTICATION_FAILED_ERROR"),
INVALID_REQUEST_IP_ADDRESS(402, "INVALID_REQUEST_IP_ADDRESS"),
NULL_POINT(404, "NULL_POINT"),
INVALID_PAPAMETER_VALUE(409, "INVALID_PAPAMETER_VALUE"),
APPLICATION_ERROR(500, "APPLICATION_ERROR"),
UNKNOWN_ERROR(99, "UNKNOWN_ERROR");
private final String message;
private int status;
ErrorCode(final int status, final String message) {
this.status = status;
this.message = message;
}
protected String getMessage() { return message; }
protected int getStatus() { return status; }
}
}
Swagger CORS에러
swagger-ui.html이 404에러가 발생해 editor로 작업
지속적인 CORS에러 발생
//MVCCONFIG
//...
@Configuration
@EnableWebMvc
public class MvcConfig extends WebMvcConfigurerAdapter {
//...
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/v2/api-docs")
.allowedOrigins("https://cancerdata.mobilecen.co.kr")
.allowedMethods("GET");
}
//...
}
Spring Boot가 아닌 Spring Framework를 사용하다보니 WvcConfig에서 CORS방지 메서드를 추가함
하지만 저 URL이 아닌 Editor에서 parameter호출시 에러가 발생하는거라 의미가 없다고 판단
URL에 다이렉트로 입력하면 정상적인 데이터 및 error코드가 나오니 형식만 맞추어 html로 export했다.
반응형
'Backend > JAVA' 카테고리의 다른 글
12/04 회고 (0) | 2023.12.04 |
---|---|
12/01 회고 (0) | 2023.12.01 |
[Java] Map의 key로 value존재 여부 확인 (0) | 2023.10.04 |
JDK 로그인 없이 다운로드 (0) | 2023.09.08 |
[Spring] Security (0) | 2023.08.24 |