Skip to content

Commit

Permalink
Merge pull request #1084 from eclipse-tractusx/feature/965-policy-man…
Browse files Browse the repository at this point in the history
…agement-error-handling

feature(chore):965 added error handling for irs response
  • Loading branch information
ds-mmaul authored Jun 20, 2024
2 parents 31044f7 + e1d0b90 commit 41de9db
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

import java.io.IOException;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -114,19 +115,17 @@ ResponseEntity<ErrorResponse> handleHttpClientErrorException(HttpClientErrorExce
try {
ResponseEntity<ErrorResponse> errorResponse = mapIRSBadRequestToErrorResponse(exception);
if (errorResponse != null) return errorResponse;
} catch (Exception e) {
ResponseEntity<ErrorResponse> body = ResponseEntity.status(BAD_REQUEST)
.body(new ErrorResponse(exception.getMessage()));
return body; // Handle the case where the message cannot be mapped to IRSErrorResponse
}
catch (Exception e) {
ResponseEntity<ErrorResponse> body = ResponseEntity.status(BAD_REQUEST)
.body(new ErrorResponse(exception.getMessage()));
return body; // Handle the case where the message cannot be mapped to IRSErrorResponse
}

} else if (status.equals(NOT_FOUND)) {
try {
ResponseEntity<ErrorResponse> errorResponse = mapIRSNotFoundToErrorResponse(exception);
if (errorResponse != null) return errorResponse;
}
catch (Exception e) {
} catch (Exception e) {
ResponseEntity<ErrorResponse> body = ResponseEntity.status(NOT_FOUND)
.body(new ErrorResponse(exception.getMessage()));
return body; // Handle the case where the message cannot be mapped to IRSErrorResponse
Expand All @@ -147,10 +146,9 @@ ResponseEntity<ErrorResponse> handleHttpClientErrorException(HttpClientErrorExce
if (jsonStart != -1 && jsonEnd != -1) {
String jsonString = rawMessage.substring(jsonStart, jsonEnd + 1);
IRSErrorResponse badRequestResponse = objectMapper.readValue(jsonString, IRSErrorResponse.class);
if (badRequestResponse != null && badRequestResponse.getMessage() != null) {
ErrorResponse errorResponse = new ErrorResponse(badRequestResponse.getMessage());
return ResponseEntity.status(400).body(errorResponse);
}
List<String> messages = badRequestResponse.getMessages();
String concatenatedMessages = String.join(", ", messages);
return ResponseEntity.status(400).body(new ErrorResponse(concatenatedMessages));
}
return null;
}
Expand All @@ -162,11 +160,10 @@ ResponseEntity<ErrorResponse> handleHttpClientErrorException(HttpClientErrorExce
int jsonEnd = rawMessage.lastIndexOf("}");
if (jsonStart != -1 && jsonEnd != -1) {
String jsonString = rawMessage.substring(jsonStart, jsonEnd + 1);
IRSErrorResponse irsError = objectMapper.readValue(jsonString, IRSErrorResponse.class);
if (irsError != null && irsError.getMessage() != null) {
ErrorResponse errorResponse = new ErrorResponse(irsError.getMessage());
return ResponseEntity.status(404).body(errorResponse);
}
IRSErrorResponse badRequestResponse = objectMapper.readValue(jsonString, IRSErrorResponse.class);
List<String> messages = badRequestResponse.getMessages();
String concatenatedMessages = String.join(", ", messages);
return ResponseEntity.status(404).body(new ErrorResponse(concatenatedMessages));
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ void shouldThorwBadRequestCreatePolicy() throws JoseException {
.post("/api/policies")
.then()
.statusCode(400)
.body(containsString("Request does not contain all required fields. Missing: odrl:permission"))
.body(containsString("Policy with id 'default-policy3342' already exists!"))
.log().all();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"message" : "Request does not contain all required fields. Missing: odrl:permission"
"statusCode": "BAD_REQUEST",
"error": "Bad Request",
"messages": [
"Policy with id 'default-policy3342' already exists!"
]
}

Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
{
"message": "404 : \"{\"statusCode\":\"NOT_FOUND\",\"error\":\"Not Found\",\"messages\":[\"Policy with id 'default-paafdsasfdolicy' not found\"]}\""
"statusCode": "NOT_FOUND",
"error": "Not Found",
"messages": [
"Policy with id 'd94e46a0-2aa2-4f62-a854-a5bc7013c78b' not found"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
import lombok.Getter;
import lombok.Setter;

import java.util.List;

@Getter
@Setter
public class IRSErrorResponse {
private String message;
private List<String> messages;
}

Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@
********************************************************************************/
package policies.response;

public record CreatePolicyResponse() {
public record CreatePolicyResponse(String policyId) {
}

0 comments on commit 41de9db

Please sign in to comment.