All Downloads are FREE. Search and download functionalities are using the official Maven repository.

uk.co.mruoc.promo.SpringExceptionHandler Maven / Gradle / Ivy

The newest version!
package uk.co.mruoc.promo;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import uk.co.mruoc.promo.entity.promo.PromoAlreadyClaimedException;
import uk.co.mruoc.promo.entity.promo.PromoFinishedException;
import uk.co.mruoc.promo.entity.promo.PromoNotFoundException;

@RequiredArgsConstructor
@ControllerAdvice
@Slf4j
public class SpringExceptionHandler {

    @ExceptionHandler(PromoAlreadyClaimedException.class)
    public ResponseEntity alreadyClaimed(PromoAlreadyClaimedException cause) {
        return new ResponseEntity<>(cause.getMessage(), HttpStatus.GONE);
    }

    @ExceptionHandler(PromoFinishedException.class)
    public ResponseEntity promoFinished(PromoFinishedException cause) {
        return new ResponseEntity<>(cause.getMessage(), HttpStatus.GONE);
    }

    @ExceptionHandler(PromoNotFoundException.class)
    public ResponseEntity promoNotFound(PromoNotFoundException cause) {
        return new ResponseEntity<>(cause.getMessage(), HttpStatus.NOT_FOUND);
    }

    @ExceptionHandler(Throwable.class)
    public ResponseEntity unexpectedError(Throwable cause) {
        log.error(cause.getMessage(), cause);
        return new ResponseEntity<>(cause.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy