diff --git a/CHANGELOG.md b/CHANGELOG.md index 28e0c2a..be0fc5f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ### Fixed +- `POST /api/backups` now answers a backup that fails with an unchecked exception (such as a `GHException` from github-api while listing repositories) with `500 Internal Server Error` and `{ "success": false, "message": "Error: ..." }`, as `COMMANDS.md` documents, instead of Spring Boot's default error body, which has no `message`, so the web UI showed only `Failed to create backup.`. No `backup-completed` event is reported for it. Previously only an `IOException` was handled this way; the command line and daemon already handled both - The interactive `backup` command now backs up each name it is given, so `backup octocat github` backs up `octocat` and then `github`, as the command line does, instead of looking up the single name `octocat github` on GitHub and failing. A name that fails is reported and the others are still backed up; the command still counts as one backup run for usage reporting, and its usage hint reads `Usage: backup [user/org2] ...` - Interactive mode's command list, printed on start and by `help`, now shows `backup [user/org2] ...` instead of `backup `, so it no longer suggests that `backup` takes a single name - A scheduled daemon backup no longer stops partway when one user or organization fails with an unchecked exception (such as a `GHException` from github-api while listing repositories): the failure is logged as an error like any other, the remaining users and organizations are still backed up, and the run is still logged and reported as completed. Previously only an `IOException` was handled, so the rest of that run was skipped until the next interval diff --git a/src/main/java/com/github/backup/web/BackupController.java b/src/main/java/com/github/backup/web/BackupController.java index 66e8997..f894c01 100644 --- a/src/main/java/com/github/backup/web/BackupController.java +++ b/src/main/java/com/github/backup/web/BackupController.java @@ -8,7 +8,6 @@ import org.springframework.web.bind.annotation.*; import jakarta.validation.Valid; -import java.io.IOException; import java.util.Collections; import java.util.Comparator; @@ -38,7 +37,8 @@ public ResponseEntity createBackup(@Valid @RequestBody BackupReq backupService.backupUserRepositories(request.getUserOrOrg()); usageReporting.backupCompleted(); return ResponseEntity.ok(new BackupResponse(true, "Backup completed successfully for " + request.getUserOrOrg())); - } catch (IOException e) { + } catch (Exception e) { + // Unchecked failures too (a GHException from github-api, say), as the CLI and daemon handle them return ResponseEntity.status(500) .body(new BackupResponse(false, "Error: " + e.getMessage())); } diff --git a/src/test/java/com/github/backup/web/BackupControllerTest.java b/src/test/java/com/github/backup/web/BackupControllerTest.java index 221c613..3893e35 100644 --- a/src/test/java/com/github/backup/web/BackupControllerTest.java +++ b/src/test/java/com/github/backup/web/BackupControllerTest.java @@ -67,6 +67,21 @@ void createBackup_Failure() throws Exception { verify(usageReporting, never()).backupCompleted(); } + @Test + void createBackup_UncheckedFailure() throws Exception { + doThrow(new IllegalStateException("Unchecked test error")).when(backupService).backupUserRepositories(anyString()); + + mockMvc.perform(post("/api/backups") + .contentType(MediaType.APPLICATION_JSON) + .content("{\"userOrOrg\":\"testuser\"}")) + .andExpect(status().isInternalServerError()) + .andExpect(jsonPath("$.success").value(false)) + .andExpect(jsonPath("$.message").value("Error: Unchecked test error")); + + verify(backupService, times(1)).backupUserRepositories("testuser"); + verify(usageReporting, never()).backupCompleted(); + } + @Test void createBackup_Success_ReportsBackupCompleted() throws Exception { mockMvc.perform(post("/api/backups")