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

return 404 if didn't find an uuid #89

Merged
merged 3 commits into from
Sep 25, 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 @@ -39,7 +39,8 @@ public ResponseEntity<Collection> getCollection(String id, String sortBy) throws
return ResponseEntity.ok()
.body(StacToCollection.convert(model.getCollections().get(0)));
} else {
throw new NoSuchElementException(String.format("uuid %s not found!", id));
log.error("UUID {} not found", id);
return ResponseEntity.notFound().build();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add more details to the returning message than simply 404 http status code?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test also failed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

after double-checking, i found this function want to return: ResponseEntity<Colleciton>, so i can only set body whose type is a Collection. For not messing up this function, i think only 404 code is enough for this situation, because the client side knows the url and uuid.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so cannot do something like this?

return ResponseEntity.status(HttpStatus.NOT_FOUND).body(String.format("uuid %s not found!", id));

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or, having a log log.error(...); before returning 404 code.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so you don't have to make the controller method returning type using raw ResponseEntity.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so cannot do something like this?

return ResponseEntity.status(HttpStatus.NOT_FOUND).body(String.format("uuid %s not found!", id));

yes this is what i tried, but it only want a Collection type for the body
image

So i use raw ResponseEntity before, not setting body for it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please try, otherwise missing out what previously tried to inform.

the client side is ok, knows the uuid and the NOT_FOUND status. For the server side output, do you think adding a log.error() is ok?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, you can just print the log if too much hassles to deal with, I think the previous impl to get some traces for what happened should stay. I've seen animaltracking controllers always use raw ResponseEntity type for the method declaration.

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@

import au.org.aodn.ogcapi.features.model.Collections;
import au.org.aodn.ogcapi.server.BaseTestClass;

import au.org.aodn.ogcapi.server.core.model.ErrorResponse;
import au.org.aodn.ogcapi.server.core.model.ExtendedCollections;
import au.org.aodn.ogcapi.server.core.model.enumeration.OGCMediaTypeMapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
Expand All @@ -28,9 +24,6 @@
@TestInstance(TestInstance.Lifecycle.PER_CLASS) // We need to use @BeforeAll @AfterAll with not static method
public class RestApiTest extends BaseTestClass {

@Autowired
protected ObjectMapper objectMapper;

@BeforeAll
public void beforeClass() {
super.createElasticIndex();
Expand Down Expand Up @@ -514,14 +507,10 @@ public void verifyCQLFuzzyKey() throws IOException {
* on any error
*/
@Test
public void verifyErrorMessageCreated() throws JsonProcessingException {
public void verifyErrorMessageCreated() {
ResponseEntity<String> collection = testRestTemplate.getForEntity(getBasePath() + "/collections/12324", String.class);

assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, collection.getStatusCode(), "Request Status match");

ErrorResponse response = objectMapper.readValue(Objects.requireNonNull(collection.getBody()), ErrorResponse.class);
assertEquals("uuid 12324 not found!", response.getMessage(), "message match");
assertEquals("uri=/api/v1/ogc/collections/12324", response.getDetails(), "message url");
assertEquals(HttpStatus.NOT_FOUND, collection.getStatusCode(), "Request Status match");
}
/**
* Test sort by, you can use this as an example for how ot use sortBy
Expand Down
Loading