-
Notifications
You must be signed in to change notification settings - Fork 21
fix: replace empty catch blocks with meaningful error handling #90
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -57,7 +57,12 @@ export function getStoredToken(): string | null { | |
| try { | ||
| const encrypted = fs.readFileSync(legacy, "utf8").trim(); | ||
| return decryptToken(encrypted); | ||
| } catch { | ||
| } catch (err) { | ||
| // file exists but can't be decrypted — warn instead of silently returning null | ||
| const detail = err instanceof Error ? err.message : String(err); | ||
| console.error( | ||
| `Warning: stored token at ${legacy} is corrupted: ${detail}`, | ||
| ); | ||
| return null; | ||
| } | ||
| } | ||
|
|
@@ -67,7 +72,11 @@ export function getStoredToken(): string | null { | |
| try { | ||
| const encrypted = fs.readFileSync(tokenPath, "utf8").trim(); | ||
| return decryptToken(encrypted); | ||
| } catch { | ||
| } catch (err) { | ||
| const detail = err instanceof Error ? err.message : String(err); | ||
| console.error( | ||
| `Warning: stored token at ${tokenPath} is corrupted: ${detail}`, | ||
| ); | ||
| return null; | ||
|
Comment on lines
72
to
80
|
||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -183,7 +183,7 @@ export class FileService { | |
| error: `File already exists: ${outputPath}. Use --overwrite to replace.`, | ||
| }; | ||
| } catch { | ||
| // File doesn't exist, we can proceed | ||
| // access() throws ENOENT when file doesn't exist — that's the expected path | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -266,10 +266,18 @@ export class FileService { | |
| // Check if file exists | ||
| try { | ||
| await access(filePath); | ||
| } catch { | ||
| } catch (err) { | ||
| // access() fails with ENOENT for missing files, EACCES for permission issues | ||
| const detail = | ||
| err instanceof Error && "code" in err | ||
| ? (err as NodeJS.ErrnoException).code | ||
| : undefined; | ||
| return { | ||
| success: false, | ||
| error: `File not found: ${filePath}`, | ||
| error: | ||
| detail === "ENOENT" | ||
| ? `File not found: ${filePath}` | ||
| : `Cannot access file: ${filePath} (${detail || (err instanceof Error ? err.message : String(err))})`, | ||
| }; | ||
|
Comment on lines
+269
to
281
|
||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -126,7 +126,10 @@ describe("downloadFile", () => { | |
|
|
||
| describe("uploadFile", () => { | ||
| it("returns error when file not found", async () => { | ||
| vi.mocked(access).mockRejectedValue(new Error("ENOENT")); | ||
| const err = Object.assign(new Error("ENOENT: no such file or directory"), { | ||
| code: "ENOENT", | ||
| }); | ||
| vi.mocked(access).mockRejectedValue(err); | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice — the mock now properly simulates a real Node.js filesystem error with it("returns descriptive error on permission denied", async () => {
const err = Object.assign(new Error("EACCES: permission denied"), {
code: "EACCES",
});
vi.mocked(access).mockRejectedValue(err);
const service = new FileService(TEST_TOKEN);
const result = await service.uploadFile("/path/to/restricted.png");
expect(result.success).toBe(false);
expect(result.error).toContain("Cannot access file");
}); |
||
| const service = new FileService(TEST_TOKEN); | ||
| const result = await service.uploadFile("/path/to/missing.png"); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Legacy token fallback catch block also logs "... is corrupted" for any readFileSync()/decryptToken() error. If the file exists but is unreadable (EACCES/EPERM) this message is misleading; consider checking errno codes and using a different warning for access errors, reserving the corruption warning for decryption failures.