Skip to content

Commit

Permalink
Update error status from 500 to 404 for commit info retrieval
Browse files Browse the repository at this point in the history
Changed the HTTP response code and description for cases where commit info is absent, aligning with proper REST conventions. Updated tests, API documentation, and removed unused constant `INTERNAL_SERVER_ERROR` accordingly.
  • Loading branch information
LazarenkoDmytro committed Dec 17, 2024
1 parent df5ff1c commit 51dcab9
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
1 change: 0 additions & 1 deletion core/src/main/java/greencity/constant/HttpStatuses.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@ public class HttpStatuses {
public static final String NOT_FOUND = "Not Found";
public static final String SEE_OTHER = "See Other";
public static final String FOUND = "Found";
public static final String INTERNAL_SERVER_ERROR = "Internal Server Error";
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io.swagger.v3.oas.annotations.media.ExampleObject;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
Expand Down Expand Up @@ -41,20 +42,20 @@ public class CommitInfoController {
"commitDate": "16/12/2024 10:55:00"
}""")))
@ApiResponse(
responseCode = "500",
description = HttpStatuses.INTERNAL_SERVER_ERROR,
responseCode = "404",
description = HttpStatuses.NOT_FOUND,
content = @Content(
mediaType = "application/json",
examples = @ExampleObject(
value = """
{
"error": "Failed to fetch commit info: Repository not found"
"error": "Git repository not initialized. Commit info is unavailable."
}""")))
@GetMapping
public ResponseEntity<CommitInfoDto> getCommitInfo() {
CommitInfoDto commitInfo = commitInfoService.getLatestCommitInfo();
if (commitInfo instanceof CommitInfoErrorDto) {
return ResponseEntity.internalServerError().body(commitInfo);
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(commitInfo);
}
return ResponseEntity.ok(commitInfo);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void getCommitInfoReturnsErrorTest() throws Exception {
when(commitInfoService.getLatestCommitInfo()).thenReturn(commitInfoDto);

mockMvc.perform(get(COMMIT_INFO_URL).accept(MediaType.APPLICATION_JSON))
.andExpect(status().isInternalServerError())
.andExpect(status().isNotFound())
.andExpect(jsonPath("$.error").value(ERROR_MESSAGE));

verify(commitInfoService, times(1)).getLatestCommitInfo();
Expand Down

0 comments on commit 51dcab9

Please sign in to comment.