Skip to content

Commit

Permalink
[PRDP-269] Changed apis' responses error codes
Browse files Browse the repository at this point in the history
  • Loading branch information
svariant committed Dec 5, 2023
1 parent 55a6d91 commit 67d5170
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 29 deletions.
17 changes: 10 additions & 7 deletions src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/GetReceipt.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package it.gov.pagopa.receipt.pdf.helpdesk;

import com.microsoft.azure.functions.ExecutionContext;
import com.microsoft.azure.functions.HttpMethod;
import com.microsoft.azure.functions.HttpRequestMessage;
import com.microsoft.azure.functions.HttpResponseMessage;
import com.microsoft.azure.functions.HttpStatus;
import com.microsoft.azure.functions.*;
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
import com.microsoft.azure.functions.annotation.BindingName;
import com.microsoft.azure.functions.annotation.FunctionName;
Expand Down Expand Up @@ -58,7 +54,7 @@ public HttpResponseMessage run(

if (eventId == null || eventId.isBlank()) {
return request
.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR)
.createResponseBuilder(HttpStatus.BAD_REQUEST)
.body(ProblemJson.builder()
.title(HttpStatus.BAD_REQUEST.name())
.detail("Please pass a valid biz-event id")
Expand All @@ -76,7 +72,14 @@ public HttpResponseMessage run(
} catch (ReceiptNotFoundException e) {
String responseMsg = String.format("Unable to retrieve the receipt with eventId %s", eventId);
logger.error("[{}] {}", context.getFunctionName(), responseMsg, e);
return request.createResponseBuilder(HttpStatus.NOT_FOUND).body(responseMsg).build();
return request
.createResponseBuilder(HttpStatus.NOT_FOUND)
.body(ProblemJson.builder()
.title(HttpStatus.NOT_FOUND.name())
.detail(responseMsg)
.status(HttpStatus.NOT_FOUND.value())
.build())
.build();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public HttpResponseMessage run(
|| iuv.isBlank()
) {
return request
.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR)
.createResponseBuilder(HttpStatus.BAD_REQUEST)
.body(ProblemJson.builder()
.title(HttpStatus.BAD_REQUEST.name())
.detail("Please pass a valid organization fiscal code and iuv")
Expand All @@ -87,7 +87,14 @@ public HttpResponseMessage run(
String responseMsg = String.format("Unable to retrieve the biz-event with organization fiscal code %s and iuv %s",
organizationFiscalCode, iuv);
logger.error("[{}] {}", context.getFunctionName(), responseMsg, e);
return request.createResponseBuilder(HttpStatus.NOT_FOUND).body(responseMsg).build();
return request
.createResponseBuilder(HttpStatus.NOT_FOUND)
.body(ProblemJson.builder()
.title(HttpStatus.NOT_FOUND.name())
.detail(responseMsg)
.status(HttpStatus.NOT_FOUND.value())
.build())
.build();
}

try {
Expand All @@ -99,7 +106,14 @@ public HttpResponseMessage run(
} catch (ReceiptNotFoundException e) {
String responseMsg = String.format("Unable to retrieve the receipt with eventId %s", bizEvent.getId());
logger.error("[{}] {}", context.getFunctionName(), responseMsg, e);
return request.createResponseBuilder(HttpStatus.NOT_FOUND).body(responseMsg).build();
return request
.createResponseBuilder(HttpStatus.NOT_FOUND)
.body(ProblemJson.builder()
.title(HttpStatus.NOT_FOUND.name())
.detail(responseMsg)
.status(HttpStatus.NOT_FOUND.value())
.build())
.build();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.microsoft.azure.functions.annotation.HttpTrigger;
import it.gov.pagopa.receipt.pdf.helpdesk.client.ReceiptCosmosClient;
import it.gov.pagopa.receipt.pdf.helpdesk.client.impl.ReceiptCosmosClientImpl;
import it.gov.pagopa.receipt.pdf.helpdesk.entity.receipt.Receipt;
import it.gov.pagopa.receipt.pdf.helpdesk.entity.receipt.ReceiptError;
import it.gov.pagopa.receipt.pdf.helpdesk.exception.ReceiptNotFoundException;
import it.gov.pagopa.receipt.pdf.helpdesk.model.ProblemJson;
Expand Down Expand Up @@ -58,7 +57,8 @@ public HttpResponseMessage run (
.body(receiptError)
.build();
} catch (ReceiptNotFoundException e) {
return request.createResponseBuilder(HttpStatus.OK)
return request
.createResponseBuilder(HttpStatus.OK)
.body(ProblemJson.builder()
.title(HttpStatus.NOT_FOUND.name())
.detail("No Receipt Error to process on bizEvent with id " + eventId)
Expand All @@ -76,7 +76,7 @@ public HttpResponseMessage run (
}
}

return request.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR)
return request.createResponseBuilder(HttpStatus.BAD_REQUEST)
.body(ProblemJson.builder()
.title(HttpStatus.BAD_REQUEST.name())
.detail("Missing valid search parameter")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,24 @@ public HttpResponseMessage run(
try {
File pdfFile = this.receiptBlobClient.getAttachmentFromBlobStorage(fileName);
FileInputStream inputStream = new FileInputStream(pdfFile);
byte [] result = IOUtils.toByteArray(inputStream);
byte[] result = IOUtils.toByteArray(inputStream);
Files.deleteIfExists(pdfFile.toPath());
return request
.createResponseBuilder(HttpStatus.OK)
.body(result)
.build();

} catch (BlobStorageClientException | IOException e) {
String responseMsg = String.format("Unable to retrieve the receipt pdf with file name %s", fileName);
logger.error("[{}] {}", context.getFunctionName(), responseMsg, e);
return request.createResponseBuilder(HttpStatus.NOT_FOUND)
return request
.createResponseBuilder(HttpStatus.NOT_FOUND)
.body(ProblemJson.builder()
.title(HttpStatus.NOT_FOUND.name())
.detail(responseMsg)
.status(HttpStatus.NOT_FOUND.value())
.build()).build();
.title(HttpStatus.NOT_FOUND.name())
.detail(responseMsg)
.status(HttpStatus.NOT_FOUND.value())
.build())
.build();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public HttpResponseMessage run (

if (eventId == null || eventId.isBlank()) {
return request
.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR)
.createResponseBuilder(HttpStatus.BAD_REQUEST)
.body(ProblemJson.builder()
.title(HttpStatus.BAD_REQUEST.name())
.detail("Please pass a valid biz-event id")
Expand All @@ -102,7 +102,11 @@ public HttpResponseMessage run (
logger.error(msg, exception);
return request
.createResponseBuilder(HttpStatus.NOT_FOUND)
.body(msg)
.body(ProblemJson.builder()
.title(HttpStatus.NOT_FOUND.name())
.detail(msg)
.status(HttpStatus.NOT_FOUND.value())
.build())
.build();
} catch (PDVTokenizerException | JsonProcessingException e) {
logger.error(e.getMessage(), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public HttpResponseMessage run (
String status = request.getQueryParameters().get("status");
if (status == null) {
return request
.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR)
.createResponseBuilder(HttpStatus.BAD_REQUEST)
.body(ProblemJson.builder()
.title(HttpStatus.BAD_REQUEST.name())
.detail("Please pass a status to recover")
Expand All @@ -95,7 +95,7 @@ public HttpResponseMessage run (
statusType = ReceiptStatusType.valueOf(status);
} catch (IllegalArgumentException e) {
return request
.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR)
.createResponseBuilder(HttpStatus.BAD_REQUEST)
.body(ProblemJson.builder()
.title(HttpStatus.BAD_REQUEST.name())
.detail("Please pass a valid status to recover")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public HttpResponseMessage run(

if (eventId == null || eventId.isBlank()) {
return request
.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR)
.createResponseBuilder(HttpStatus.BAD_REQUEST)
.body(ProblemJson.builder()
.title(HttpStatus.BAD_REQUEST.name())
.detail("Please pass a valid biz-event id")
Expand All @@ -89,7 +89,14 @@ public HttpResponseMessage run(
} catch (ReceiptNotFoundException e) {
String responseMsg = String.format("Unable to retrieve the receipt with eventId %s", eventId);
logger.error("[{}] {}", context.getFunctionName(), responseMsg, e);
return request.createResponseBuilder(HttpStatus.NOT_FOUND).body(responseMsg).build();
return request
.createResponseBuilder(HttpStatus.NOT_FOUND)
.body(ProblemJson.builder()
.title(HttpStatus.NOT_FOUND.name())
.detail(responseMsg)
.status(HttpStatus.NOT_FOUND.value())
.build())
.build();
}

if (!receipt.getStatus().equals(ReceiptStatusType.IO_ERROR_TO_NOTIFY) && !receipt.getStatus().equals(ReceiptStatusType.GENERATED)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public HttpResponseMessage run(
String status = request.getQueryParameters().get("status");
if (status == null) {
return request
.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR)
.createResponseBuilder(HttpStatus.BAD_REQUEST)
.body(ProblemJson.builder()
.title(HttpStatus.BAD_REQUEST.name())
.detail("Please pass a status to recover")
Expand All @@ -88,7 +88,7 @@ public HttpResponseMessage run(
statusType = ReceiptStatusType.valueOf(status);
} catch (IllegalArgumentException e) {
return request
.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR)
.createResponseBuilder(HttpStatus.BAD_REQUEST)
.body(ProblemJson.builder()
.title(HttpStatus.BAD_REQUEST.name())
.detail("Please pass a valid status to recover")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public HttpResponseMessage run (

}

return request.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR)
return request.createResponseBuilder(HttpStatus.BAD_REQUEST)
.body(ProblemJson.builder()
.title(HttpStatus.BAD_REQUEST.name())
.detail("Missing valid eventId paramater")
Expand All @@ -143,7 +143,7 @@ public HttpResponseMessage run (
message = "BizEvent not found with event-id " + eventId;
}
return request
.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR)
.createResponseBuilder(HttpStatus.BAD_REQUEST)
.body(ProblemJson.builder()
.title(HttpStatus.BAD_REQUEST.name())
.detail(message)
Expand Down

0 comments on commit 67d5170

Please sign in to comment.