Projekt

Obecné

Profil

Stáhnout (2.11 KB) Statistiky
| Větev: | Tag: | Revize:
1
package cz.zcu.kiv.backendapi.exception;
2

    
3

    
4
import org.springframework.http.HttpStatus;
5
import org.springframework.http.ResponseEntity;
6
import org.springframework.security.core.userdetails.UsernameNotFoundException;
7
import org.springframework.validation.BindException;
8
import org.springframework.web.bind.annotation.ControllerAdvice;
9
import org.springframework.web.bind.annotation.ExceptionHandler;
10

    
11
/**
12
 * Exception handler
13
 */
14
@ControllerAdvice
15
public class ApiExceptionHandler {
16

    
17
    /**
18
     * Handles bind exception
19
     *
20
     * @param exception bind exception
21
     * @return response entity with message and status
22
     */
23
    @ExceptionHandler(value = {BindException.class})
24
    public ResponseEntity<Object> handleBindException(BindException exception) {
25
        return new ResponseEntity<>(exception.getBindingResult().getAllErrors(), HttpStatus.BAD_REQUEST);
26
    }
27

    
28
    /**
29
     * Handles username not found exception
30
     *
31
     * @param exception username not found exception
32
     * @return response entity with message and status
33
     */
34
    @ExceptionHandler(value = {UsernameNotFoundException.class})
35
    public ResponseEntity<Object> handleUsernameNotFoundException(UsernameNotFoundException exception) {
36
        return new ResponseEntity<>(exception.getMessage(), HttpStatus.NOT_FOUND);
37
    }
38

    
39
    /**
40
     * Handles api request exception
41
     *
42
     * @param exception api request found exception
43
     * @return response entity with message and status
44
     */
45
    @ExceptionHandler(value = {ApiRequestException.class})
46
    public ResponseEntity<Object> handleApiRequestException(ApiRequestException exception) {
47
        return new ResponseEntity<>(exception.getMessage(), exception.getHttpStatus());
48
    }
49

    
50
    /**
51
     * Handles illegal argument exception
52
     *
53
     * @param exception illegal argument exception
54
     * @return response entity with message and status
55
     */
56
    @ExceptionHandler(value = {IllegalArgumentException.class})
57
    public ResponseEntity<Object> handleIllegalArgumentException(IllegalArgumentException exception) {
58
        return new ResponseEntity<>(exception.getMessage(), HttpStatus.BAD_REQUEST);
59
    }
60
}
(1-1/2)