Skip to content

Commit

Permalink
implement BadRequestException handling
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdunnjpl committed Jul 19, 2024
1 parent c00412b commit 0547f29
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ private PdsLidVid resolveIdentifierToLidvid(PdsProductIdentifier identifier) thr
@Override
public ResponseEntity<Object> productMembers(
String identifier, List<String> fields, Integer limit, List<String> sort, List<String> searchAfter)
throws NotFoundException, UnhandledException, SortSearchAfterMismatchException, MiscellaneousBadRequestException,
throws NotFoundException, UnhandledException, SortSearchAfterMismatchException, BadRequestException,
AcceptFormatNotSupportedException{

try{
Expand All @@ -423,7 +423,7 @@ public ResponseEntity<Object> productMembers(
searchRequestBuilder.matchMembersOfCollection(lidvid);
searchRequestBuilder.onlyBasicProducts();
} else {
throw new MiscellaneousBadRequestException("productMembers endpoint is only valid for products with Product_Class '" +
throw new BadRequestException("productMembers endpoint is only valid for products with Product_Class '" +
PdsProductClasses.Product_Bundle + "' or '" + PdsProductClasses.Product_Collection +
"' (got '" + productClass + "')");
}
Expand All @@ -449,7 +449,7 @@ public ResponseEntity<Object> productMembers(
@Override
public ResponseEntity<Object> productMembersMembers(
String identifier, List<String> fields, Integer limit, List<String> sort, List<String> searchAfter)
throws NotFoundException, UnhandledException, SortSearchAfterMismatchException, MiscellaneousBadRequestException,
throws NotFoundException, UnhandledException, SortSearchAfterMismatchException, BadRequestException,
AcceptFormatNotSupportedException{

try{
Expand All @@ -463,7 +463,7 @@ public ResponseEntity<Object> productMembersMembers(
searchRequestBuilder.matchMembersOfBundle(lidvid);
searchRequestBuilder.onlyBasicProducts();
} else {
throw new MiscellaneousBadRequestException("productMembers endpoint is only valid for products with Product_Class '" +
throw new BadRequestException("productMembers endpoint is only valid for products with Product_Class '" +
PdsProductClasses.Product_Bundle + "' (got '" + productClass + "')");
}

Expand Down Expand Up @@ -524,7 +524,7 @@ private List<PdsLidVid> resolveLidVidsFromProductField(PdsProductIdentifier iden
@Override
public ResponseEntity<Object> productMemberOf(
String identifier, List<String> fields, Integer limit, List<String> sort, List<String> searchAfter)
throws NotFoundException, UnhandledException, SortSearchAfterMismatchException, MiscellaneousBadRequestException,
throws NotFoundException, UnhandledException, SortSearchAfterMismatchException, BadRequestException,
AcceptFormatNotSupportedException{

try{
Expand All @@ -538,7 +538,7 @@ public ResponseEntity<Object> productMemberOf(
} else if (productClass.isBasicProduct()) {
parentIds = resolveLidVidsFromProductField(lidvid, "ops:Provenance/ops:parent_collection_identifier");
} else {
throw new MiscellaneousBadRequestException("productMembersOf endpoint is not valid for products with Product_Class '" +
throw new BadRequestException("productMembersOf endpoint is not valid for products with Product_Class '" +
PdsProductClasses.Product_Bundle + "' (got '" + productClass + "')");
}

Expand All @@ -564,7 +564,7 @@ public ResponseEntity<Object> productMemberOf(
@Override
public ResponseEntity<Object> productMemberOfOf(
String identifier, List<String> fields, Integer limit, List<String> sort, List<String> searchAfter)
throws NotFoundException, UnhandledException, SortSearchAfterMismatchException, MiscellaneousBadRequestException,
throws NotFoundException, UnhandledException, SortSearchAfterMismatchException, BadRequestException,
AcceptFormatNotSupportedException{

try{
Expand All @@ -577,7 +577,7 @@ public ResponseEntity<Object> productMemberOfOf(
parentIds = resolveLidVidsFromProductField(lidvid, "ops:Provenance/ops:parent_bundle_identifier");
} else {
// TODO: replace with enumeration of acceptable values later
throw new MiscellaneousBadRequestException("productMembersOf endpoint is not valid for products with Product_Class '" +
throw new BadRequestException("productMembersOf endpoint is not valid for products with Product_Class '" +
PdsProductClasses.Product_Bundle + "' or '" + PdsProductClasses.Product_Collection + "' (got '" + productClass + "')");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,15 @@


import java.util.Set;

import gov.nasa.pds.api.registry.model.exceptions.*;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
import gov.nasa.pds.api.registry.model.exceptions.AcceptFormatNotSupportedException;
import gov.nasa.pds.api.registry.model.exceptions.SortSearchAfterMismatchException;
import gov.nasa.pds.api.registry.model.exceptions.NotFoundException;
import gov.nasa.pds.api.registry.model.exceptions.RegistryApiException;
import gov.nasa.pds.api.registry.model.exceptions.UnhandledException;
import gov.nasa.pds.api.registry.model.exceptions.UnparsableQParamException;



@ControllerAdvice
Expand Down Expand Up @@ -49,6 +44,12 @@ protected ResponseEntity<Object> notFound(NotFoundException ex, WebRequest reque

}

@ExceptionHandler(value = {BadRequestException.class})
protected ResponseEntity<Object> badRequest(BadRequestException ex, WebRequest request) {
return genericExceptionHandler(ex, request, "", HttpStatus.BAD_REQUEST);

}

@ExceptionHandler(value = {UnhandledException.class})
protected ResponseEntity<Object> unhandled(UnhandledException ex, WebRequest request) {
return genericExceptionHandler(ex, request, "", HttpStatus.INTERNAL_SERVER_ERROR);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package gov.nasa.pds.api.registry.model.exceptions;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serial;

public class BadRequestException extends RegistryApiException {
private static final Logger log = LoggerFactory.getLogger(BadRequestException.class);
@Serial
private static final long serialVersionUID = 2026697251322082840L;

public BadRequestException(String msg) {
super("BadRequestException: " + msg);
}

}

This file was deleted.

0 comments on commit 0547f29

Please sign in to comment.