-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGlobalExceptionHandler.java
More file actions
114 lines (95 loc) · 4.19 KB
/
GlobalExceptionHandler.java
File metadata and controls
114 lines (95 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package com.lcl.wsctxd04.exceptions;
import com.lcl.wsctxd04.models.ErrorResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
/**
* Gestion centralisée des exceptions.
*
* Codes de retour validation :
* - B001 (400) : champ obligatoire absent ou vide
* - B002 (400) : champ présent mais dépasse la taille maximale
* - UNKNOWN_ERROR (500) : erreur technique CICS
*/
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
private static final String CODE_SEPARATOR = ":";
/**
* Validation @Valid → retourne 400 avec le code B001 ou B002.
*/
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<ErrorResponse> handleValidationErrors(MethodArgumentNotValidException ex) {
var fieldError = ex.getBindingResult().getFieldErrors().stream()
.min((a, b) -> extractCode(a.getDefaultMessage())
.compareTo(extractCode(b.getDefaultMessage())))
.orElse(null);
if (fieldError == null) {
return ResponseEntity.badRequest()
.body(new ErrorResponse("B001", "Validation error"));
}
String code = extractCode(fieldError.getDefaultMessage());
String message = extractMessage(fieldError.getDefaultMessage());
log.warn("Validation failed [{}] on field '{}': {}", code, fieldError.getField(), message);
return ResponseEntity
.status(HttpStatus.BAD_REQUEST)
.body(new ErrorResponse(code, message));
}
/**
* Erreur interne WSCTXD04 → 500.
*/
@ExceptionHandler(InternalWSCTXD04Exception.class)
public ResponseEntity<ErrorResponse> handleInternalException(InternalWSCTXD04Exception ex) {
log.error("CICS communication error: {}", ex.getMessage(), ex);
return ResponseEntity
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(new ErrorResponse("UNKNOWN_ERROR",
"Error occurred while communicating with CICS Service. "
+ "Please ensure that the CICS Service is operational."));
}
/**
* Erreur transactionnelle CICS → 500 avec le code spécifique.
*/
@ExceptionHandler(TransactionException.class)
public ResponseEntity<ErrorResponse> handleTransactionException(TransactionException ex) {
log.error("Transaction error [{}]: {}", ex.getCode(), ex.getMessage(), ex);
return ResponseEntity
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(new ErrorResponse(ex.getCode(), ex.getMessage()));
}
/**
* Erreur service métier → 400 avec le code spécifique.
*/
@ExceptionHandler(ServiceException.class)
public ResponseEntity<ErrorResponse> handleServiceException(ServiceException ex) {
log.error("Service error [{}]: {}", ex.getCode(), ex.getMessage(), ex);
return ResponseEntity
.status(HttpStatus.BAD_REQUEST)
.body(new ErrorResponse(ex.getCode(), ex.getMessage()));
}
/**
* Filet de sécurité → 500.
*/
@ExceptionHandler(Exception.class)
public ResponseEntity<ErrorResponse> handleUnexpectedException(Exception ex) {
log.error("Unexpected error: {}", ex.getMessage(), ex);
return ResponseEntity
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(new ErrorResponse("UNKNOWN_ERROR", "An unexpected error occurred."));
}
private String extractCode(String rawMessage) {
if (rawMessage != null && rawMessage.contains(CODE_SEPARATOR)) {
return rawMessage.substring(0, rawMessage.indexOf(CODE_SEPARATOR));
}
return "B001";
}
private String extractMessage(String rawMessage) {
if (rawMessage != null && rawMessage.contains(CODE_SEPARATOR)) {
return rawMessage.substring(rawMessage.indexOf(CODE_SEPARATOR) + 1);
}
return rawMessage;
}
}