Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature(chore):965 added error handling for irs response #1084

Merged
merged 2 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
}
Loading