Skip to content

Commit f4bb7c4

Browse files
error decoder updated
1 parent bb5b262 commit f4bb7c4

File tree

5 files changed

+51
-47
lines changed

5 files changed

+51
-47
lines changed

auth-service/src/main/java/com/safalifter/authservice/client/CustomErrorDecoder.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.safalifter.authservice.client;
22

3+
import com.fasterxml.jackson.databind.ObjectMapper;
34
import com.safalifter.authservice.exc.GenericErrorResponse;
5+
import com.safalifter.authservice.exc.ValidationException;
46
import feign.Response;
57
import feign.codec.ErrorDecoder;
68
import org.apache.commons.io.IOUtils;
@@ -9,23 +11,24 @@
911
import java.io.IOException;
1012
import java.io.InputStream;
1113
import java.nio.charset.StandardCharsets;
12-
14+
import java.util.Map;
1315
public class CustomErrorDecoder implements ErrorDecoder {
16+
private final ObjectMapper mapper = new ObjectMapper();
17+
1418
@Override
1519
public Exception decode(String methodKey, Response response) {
16-
if (response.body() == null) // 401-403
17-
throw GenericErrorResponse.builder()
18-
.httpStatus(HttpStatus.valueOf(response.status()))
19-
.message("No response body")
20-
.build();
21-
2220
try (InputStream body = response.body().asInputStream()) {
23-
String message = IOUtils.toString(body, StandardCharsets.UTF_8);
24-
return GenericErrorResponse
25-
.builder()
26-
.httpStatus(HttpStatus.valueOf(response.status()))
27-
.message(message.substring(10, message.length() - 2))
28-
.build();
21+
Map<String, String> errors =
22+
mapper.readValue(IOUtils.toString(body, StandardCharsets.UTF_8), Map.class);
23+
if (response.status() == 400) {
24+
return ValidationException.builder()
25+
.validationErrors(errors).build();
26+
} else
27+
return GenericErrorResponse
28+
.builder()
29+
.httpStatus(HttpStatus.valueOf(response.status()))
30+
.message(errors.get("error"))
31+
.build();
2932

3033
} catch (IOException exception) {
3134
throw GenericErrorResponse.builder()
Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,21 @@
11
package com.safalifter.authservice.exc;
22

3-
import org.springframework.http.HttpHeaders;
43
import org.springframework.http.HttpStatus;
54
import org.springframework.http.ResponseEntity;
6-
import org.springframework.validation.FieldError;
7-
import org.springframework.web.bind.MethodArgumentNotValidException;
85
import org.springframework.web.bind.annotation.ExceptionHandler;
96
import org.springframework.web.bind.annotation.RestControllerAdvice;
10-
import org.springframework.web.context.request.WebRequest;
117
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
128

139
import java.util.HashMap;
1410
import java.util.Map;
1511

1612
@RestControllerAdvice
1713
public class GeneralExceptionHandler extends ResponseEntityExceptionHandler {
18-
19-
@Override
20-
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex,
21-
HttpHeaders headers, HttpStatus status, WebRequest request) {
14+
@ExceptionHandler(Exception.class)
15+
public final ResponseEntity<?> handleAllException(Exception ex) {
2216
Map<String, String> errors = new HashMap<>();
23-
ex.getBindingResult().getAllErrors()
24-
.forEach(x -> errors.put(((FieldError) x).getField(), x.getDefaultMessage()));
25-
return ResponseEntity.badRequest().body(errors);
17+
errors.put("error", ex.getMessage());
18+
return new ResponseEntity<>(errors, HttpStatus.BAD_REQUEST);
2619
}
2720

2821
@ExceptionHandler(GenericErrorResponse.class)
@@ -32,17 +25,15 @@ public ResponseEntity<?> genericError(GenericErrorResponse exception) {
3225
return new ResponseEntity<>(errors, exception.getHttpStatus());
3326
}
3427

35-
@ExceptionHandler(Exception.class)
36-
public final ResponseEntity<?> handleAllException(Exception ex, WebRequest request) {
37-
Map<String, String> errors = new HashMap<>();
38-
errors.put("error", ex.getMessage());
39-
return new ResponseEntity<>(errors, HttpStatus.BAD_REQUEST);
40-
}
41-
4228
@ExceptionHandler(WrongCredentialsException.class)
4329
public ResponseEntity<?> usernameOrPasswordInvalidException(WrongCredentialsException exception) {
4430
Map<String, String> errors = new HashMap<>();
4531
errors.put("error", exception.getMessage());
4632
return new ResponseEntity<>(errors, HttpStatus.UNAUTHORIZED);
4733
}
34+
35+
@ExceptionHandler(ValidationException.class)
36+
public ResponseEntity<?> validationException(ValidationException exception) {
37+
return ResponseEntity.badRequest().body(exception.getValidationErrors());
38+
}
4839
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.safalifter.authservice.exc;
2+
3+
import lombok.Builder;
4+
import lombok.Getter;
5+
6+
import java.util.Map;
7+
8+
@Builder
9+
@Getter
10+
public class ValidationException extends RuntimeException {
11+
private Map<String, String> validationErrors;
12+
}

job-service/src/main/java/com/safalifter/jobservice/client/CustomErrorDecoder.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.safalifter.jobservice.client;
22

3+
import com.fasterxml.jackson.databind.ObjectMapper;
34
import com.safalifter.jobservice.exc.GenericErrorResponse;
45
import feign.Response;
56
import feign.codec.ErrorDecoder;
@@ -9,22 +10,20 @@
910
import java.io.IOException;
1011
import java.io.InputStream;
1112
import java.nio.charset.StandardCharsets;
13+
import java.util.Map;
1214

1315
public class CustomErrorDecoder implements ErrorDecoder {
16+
private final ObjectMapper mapper = new ObjectMapper();
17+
1418
@Override
1519
public Exception decode(String methodKey, Response response) {
16-
if (response.body() == null) // 401-403
17-
throw GenericErrorResponse.builder()
18-
.httpStatus(HttpStatus.valueOf(response.status()))
19-
.message("No response body")
20-
.build();
21-
2220
try (InputStream body = response.body().asInputStream()) {
23-
String message = IOUtils.toString(body, StandardCharsets.UTF_8);
21+
Map<String, String> errors =
22+
mapper.readValue(IOUtils.toString(body, StandardCharsets.UTF_8), Map.class);
2423
return GenericErrorResponse
2524
.builder()
2625
.httpStatus(HttpStatus.valueOf(response.status()))
27-
.message(message.substring(10, message.length() - 2))
26+
.message(errors.get("error"))
2827
.build();
2928

3029
} catch (IOException exception) {

user-service/src/main/java/com/safalifter/userservice/client/CustomErrorDecoder.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.safalifter.userservice.client;
22

3+
import com.fasterxml.jackson.databind.ObjectMapper;
34
import com.safalifter.userservice.exc.GenericErrorResponse;
45
import feign.Response;
56
import feign.codec.ErrorDecoder;
@@ -9,22 +10,20 @@
910
import java.io.IOException;
1011
import java.io.InputStream;
1112
import java.nio.charset.StandardCharsets;
13+
import java.util.Map;
1214

1315
public class CustomErrorDecoder implements ErrorDecoder {
16+
private final ObjectMapper mapper = new ObjectMapper();
17+
1418
@Override
1519
public Exception decode(String methodKey, Response response) {
16-
if (response.body() == null) // 401-403
17-
throw GenericErrorResponse.builder()
18-
.httpStatus(HttpStatus.valueOf(response.status()))
19-
.message("No response body")
20-
.build();
21-
2220
try (InputStream body = response.body().asInputStream()) {
23-
String message = IOUtils.toString(body, StandardCharsets.UTF_8);
21+
Map<String, String> errors =
22+
mapper.readValue(IOUtils.toString(body, StandardCharsets.UTF_8), Map.class);
2423
return GenericErrorResponse
2524
.builder()
2625
.httpStatus(HttpStatus.valueOf(response.status()))
27-
.message(message.substring(10, message.length() - 2))
26+
.message(errors.get("error"))
2827
.build();
2928

3029
} catch (IOException exception) {

0 commit comments

Comments
 (0)