mirror of
https://github.com/whyour/qinglong.git
synced 2026-02-12 14:05:38 +08:00
Improve error handling based on code review feedback
- Use finally block to ensure file handle is closed even on error - Extract error messages properly using instanceof Error check - Improve error message formatting for better debugging Co-authored-by: whyour <22700758+whyour@users.noreply.github.com>
This commit is contained in:
parent
82514e65e1
commit
e1ce0f3fa9
|
|
@ -21,13 +21,19 @@ export async function writeFileWithLock(
|
|||
options = { encoding: options };
|
||||
}
|
||||
|
||||
try {
|
||||
// Ensure file exists before locking
|
||||
if (!(await fileExist(filePath))) {
|
||||
const fileHandle = await open(filePath, 'w');
|
||||
let fileHandle;
|
||||
try {
|
||||
fileHandle = await open(filePath, 'w');
|
||||
} catch (error) {
|
||||
const errorMessage = error instanceof Error ? error.message : String(error);
|
||||
throw new Error(`Failed to create file ${filePath}: ${errorMessage}`);
|
||||
} finally {
|
||||
if (fileHandle) {
|
||||
await fileHandle.close();
|
||||
}
|
||||
} catch (error) {
|
||||
throw new Error(`Failed to create file ${filePath}: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
const lockfilePath = getUniqueLockPath(filePath);
|
||||
|
|
@ -44,7 +50,8 @@ export async function writeFileWithLock(
|
|||
lockfilePath,
|
||||
});
|
||||
} catch (error) {
|
||||
throw new Error(`Failed to acquire lock for ${filePath}: ${error}`);
|
||||
const errorMessage = error instanceof Error ? error.message : String(error);
|
||||
throw new Error(`Failed to acquire lock for ${filePath}: ${errorMessage}`);
|
||||
}
|
||||
|
||||
try {
|
||||
|
|
@ -53,7 +60,8 @@ export async function writeFileWithLock(
|
|||
await chmod(filePath, options.mode);
|
||||
}
|
||||
} catch (error) {
|
||||
throw new Error(`Failed to write to file ${filePath}: ${error}`);
|
||||
const errorMessage = error instanceof Error ? error.message : String(error);
|
||||
throw new Error(`Failed to write to file ${filePath}: ${errorMessage}`);
|
||||
} finally {
|
||||
if (release) {
|
||||
try {
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user