Skip to content

Commit

Permalink
refactor: consistent access check handling and error logging
Browse files Browse the repository at this point in the history
  • Loading branch information
kukhariev committed Apr 26, 2024
1 parent 2a64212 commit acac428
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 14 deletions.
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

0 comments on commit acac428

Please sign in to comment.