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

refactor: consistent access check handling and error logging #673

Merged
merged 1 commit into from
Apr 26, 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
10 changes: 6 additions & 4 deletions packages/core/src/storages/disk-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,12 @@ export class DiskStorage extends BaseStorage<DiskFile> {
const metaConfig = { ...config, ...config.metaStorageConfig };
this.meta = new LocalMetaStorage(metaConfig);
}
this.accessCheck().catch(err => {
this.isReady = false;
this.logger.error('[error]: Could not write to directory: %O', err);
});
this.isReady = false;
this.accessCheck()
.then(() => (this.isReady = true))
.catch(err => {
this.logger.error('Storage access check failed: %O', err);
});
}

normalizeError(err: Error): HttpError {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/storages/local-meta-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class LocalMetaStorage<T extends File = File> extends MetaStorage<T> {
super(config);
this.directory = (config?.directory || join(tmpdir(), 'uploadx_meta')).replace(/\\/g, '/');
this.accessCheck().catch(err => {
this.logger.error('[error]: Could not write to directory: %O', err);
this.logger.error('Metadata storage access check failed: %O', err);
});
}

Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/utils/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ export async function ensureFile(path: string, overwrite = false): Promise<numbe
}

export async function accessCheck(dir: string): Promise<void> {
await fsp.mkdir(dir, { recursive: true });
try {
await fsp.mkdir(dir, { recursive: true });
} catch {
throw new Error(`Directory is not accessible: ${dir}`);
}
}

/**
Expand Down
8 changes: 4 additions & 4 deletions packages/gcs/src/gcs-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,10 @@ export class GCStorage extends BaseStorage<GCSFile> {
config.scopes ||= GCSConfig.authScopes;
this.authClient = new GoogleAuth(config);

this.accessCheck().catch((err: ClientError) => {
this.isReady = false;
this.logger.error('Unable to open bucket: %O', err);
});
this.isReady = false;
this.accessCheck()
.then(() => (this.isReady = true))
.catch(err => this.logger.error('Storage access check failed: %O', err));
}

normalizeError(error: ClientError): HttpError {
Expand Down
8 changes: 4 additions & 4 deletions packages/s3/src/s3-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,10 @@ export class S3Storage extends BaseStorage<S3File> {
? new LocalMetaStorage(metaConfig)
: new S3MetaStorage(metaConfig);
}
this.accessCheck().catch((err: AWSError) => {
this.isReady = false;
this.logger.error('Unable to open bucket: %O', err);
});
this.isReady = false;
this.accessCheck()
.then(() => (this.isReady = true))
.catch(err => this.logger.error('Storage access check failed: %O', err));
}

normalizeError(error: AWSError): HttpError {
Expand Down
Loading