Skip to content

Commit

Permalink
Added few more catches and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
andrea-barchi committed Aug 25, 2023
1 parent e9ebd2f commit 663c771
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 11 deletions.
33 changes: 29 additions & 4 deletions src/main/java/it/gov/pagopa/taxonomy/TaxonomyGetFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.azure.storage.blob.BlobContainerClient;
import com.azure.storage.blob.BlobServiceClient;
import com.azure.storage.blob.BlobServiceClientBuilder;
import com.azure.storage.blob.models.BlobErrorCode;
import com.azure.storage.blob.models.BlobStorageException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -89,7 +91,7 @@ public HttpResponseMessage getTaxonomy(
.build());

return AppUtil.writeResponse(request,
HttpStatus.NOT_IMPLEMENTED,
HttpStatus.BAD_REQUEST,
payload);
}

Expand All @@ -115,9 +117,32 @@ public HttpResponseMessage getTaxonomy(
.error(e.getCodeMessage().message(e.getArgs()))
.build());
return AppUtil.writeResponse(request,
HttpStatus.valueOf(e.getCodeMessage().httpStatus().name()),
payload);

HttpStatus.valueOf(e.getCodeMessage().httpStatus().name()),
payload);
} catch (BlobStorageException e) {
if(e.getErrorCode().equals(BlobErrorCode.BLOB_NOT_FOUND)) {
logger.log(Level.SEVERE, "[ALERT][Get] BlobStorageException at " + Instant.now() + "\n" + ExceptionUtils.getStackTrace(e), e);
AppException appException = new AppException(e, AppErrorCodeMessageEnum.BLOB_NOT_FOUND_JSON_ERROR);
String payload = AppUtil.getPayload(getObjectMapper(), ErrorMessage.builder()
.message("Taxonomy retrieval failed")
.error(appException.getCodeMessage().message(appException.getArgs()))
.build());
return AppUtil.writeResponse(request,
HttpStatus.valueOf(appException.getCodeMessage().httpStatus().name()),
payload
);
} else {
logger.log(Level.SEVERE, "[ALERT][Get] BlobStorageException at " + Instant.now() + "\n" + ExceptionUtils.getStackTrace(e), e);
AppException appException = new AppException(e, AppErrorCodeMessageEnum.ERROR);
String payload = AppUtil.getPayload(getObjectMapper(), ErrorMessage.builder()
.message("Taxonomy retrieval failed")
.error(appException.getCodeMessage().message(appException.getArgs()))
.build());
return AppUtil.writeResponse(request,
HttpStatus.valueOf(appException.getCodeMessage().httpStatus().name()),
payload
);
}
} catch (Exception e) {
logger.log(Level.SEVERE, MessageFormat.format("[ALERT][Get] GenericError at {0}\n {1}", Instant.now(), ExceptionUtils.getMessage(e)));

Expand Down
41 changes: 34 additions & 7 deletions src/main/java/it/gov/pagopa/taxonomy/TaxonomyUpdateFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.azure.storage.blob.BlobContainerClient;
import com.azure.storage.blob.BlobServiceClient;
import com.azure.storage.blob.BlobServiceClientBuilder;
import com.azure.storage.blob.models.BlobErrorCode;
import com.azure.storage.blob.models.BlobStorageException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
Expand Down Expand Up @@ -105,17 +107,42 @@ public HttpResponseMessage updateTaxonomy(
} catch (AppException e) {
logger.log(Level.SEVERE, MessageFormat.format("[ALERT][Get][Triggered] AppException at {0}\n {1}",Instant.now(), ExceptionUtils.getStackTrace(e)));
String payload = AppUtil.getPayload(getObjectMapper(), ErrorMessage.builder()
.message("Taxonomy update failed")
.error(e.getCodeMessage().message(e.getArgs()))
.build());
.message("Taxonomy update failed")
.error(e.getCodeMessage().message(e.getArgs()))
.build());
return AppUtil.writeResponse(request,
HttpStatus.valueOf(e.getCodeMessage().httpStatus().name()),
payload
);

HttpStatus.valueOf(e.getCodeMessage().httpStatus().name()),
payload
);

} catch (BlobStorageException e) {
if(e.getErrorCode().equals(BlobErrorCode.BLOB_NOT_FOUND)) {
logger.log(Level.SEVERE, "[ALERT][Update] BlobStorageException at " + Instant.now() + "\n" + ExceptionUtils.getStackTrace(e), e);
AppException appException = new AppException(e, AppErrorCodeMessageEnum.BLOB_NOT_FOUND_CSV_ERROR);
String payload = AppUtil.getPayload(getObjectMapper(), ErrorMessage.builder()
.message("Taxonomy update failed")
.error(appException.getCodeMessage().message(appException.getArgs()))
.build());
return AppUtil.writeResponse(request,
HttpStatus.valueOf(appException.getCodeMessage().httpStatus().name()),
payload
);
} else {
logger.log(Level.SEVERE, "[ALERT][Update] BlobStorageException at " + Instant.now() + "\n" + ExceptionUtils.getStackTrace(e), e);
AppException appException = new AppException(e, AppErrorCodeMessageEnum.ERROR);
String payload = AppUtil.getPayload(getObjectMapper(), ErrorMessage.builder()
.message("Taxonomy update failed")
.error(appException.getCodeMessage().message(appException.getArgs()))
.build());
return AppUtil.writeResponse(request,
HttpStatus.valueOf(appException.getCodeMessage().httpStatus().name()),
payload
);
}
} catch (Exception e) {
logger.log(Level.SEVERE,MessageFormat.format("[ALERT][Get][Triggered] Generic error at {0}\n {1}",Instant.now(), ExceptionUtils.getStackTrace(e)));

logger.log(Level.SEVERE, "[ALERT][Update] GenericError at " + Instant.now() + "\n" + ExceptionUtils.getStackTrace(e), e);
AppException appException = new AppException(e, AppErrorCodeMessageEnum.ERROR);
String payload = AppUtil.getPayload(getObjectMapper(), ErrorMessage.builder()
.message("Taxonomy update failed")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
import it.gov.pagopa.taxonomy.util.AppMessageUtil;

import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.Response.Status;


public enum AppErrorCodeMessageEnum implements AppErrorCodeMessageInterface {

// ERRORS
ERROR("0500", "system.error", Response.Status.INTERNAL_SERVER_ERROR),
BLOB_NOT_FOUND_CSV_ERROR("0404", "blob.not.found.csv.error", Status.NOT_FOUND),
BLOB_NOT_FOUND_JSON_ERROR("0404", "blob.not.found.json.error", Status.NOT_FOUND),
CSV_PARSING_ERROR("0103", "csv.parsing.error", Response.Status.INTERNAL_SERVER_ERROR),
JSON_PARSING_ERROR("0107", "json.parsing.error", Response.Status.INTERNAL_SERVER_ERROR);

Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/messages.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
system.error=An unexpected error has occurred. Please contact support.
csv.parsing.error=Error when parsing the CSV file.
json.parsing.error=Error when parsing the JSON file.
blob.not.found.csv.error=Cannot find CSV file.
blob.not.found.json.error=Cannot find JSON file.
generic.retrieval.error=Taxonomy retrieval failed.
version.not.exists.error=Unknown version has been specified.

0 comments on commit 663c771

Please sign in to comment.