Spring Instagram clone (Exception handler)

Hyeonseop-Noh·2022년 4월 5일
0

CustomValidationApiException

In previous post, CustomValidationException catch the signup validation exception (username, password, email, name)

For the Edit user info, create CustomValidationApiException.

public class CustomValidationApiException extends RuntimeException {

    private static final long serialVersionUID = 1L;

    private Map<String, String> errorMap;

    public CustomValidationApiException(String message, Map<String, String> errorMap) {
        super(message);
        this.errorMap = errorMap;
    }

    public CustomValidationApiException(String message) {
        super(message);
    }

    public Map<String, String> getErrorMap() {
        return errorMap;
    }

}

ControllerExceptionHandler

Rewrite to

@ExceptionHandler(CustomValidationException.class)
public String validationException(CustomValidationException e) {
    return Script.back(e.getErrorMap().toString()); // return javascript
}

@ExceptionHandler(CustomValidationApiException.class)
public ResponseEntity<CMRespDto<?>> validationApiException(CustomValidationApiException e) {
    return new ResponseEntity<>(new CMRespDto<>(-1, e.getMessage(), e.getErrorMap()), HttpStatus.BAD_REQUEST); // return data
}

note) Comparison of CMRespDto and Script
1. Response with client - Script
2. Ajax communication - CMRespDto
3. Android communication - CMRespDto

profile
PlanBy Developer

0개의 댓글