fix(core): Refactor Bucket class for improved error handling

This commit is contained in:
Philipp Kunz 2024-11-24 03:05:10 +01:00
parent 0b396f19cf
commit 16a82ac50a
3 changed files with 25 additions and 9 deletions

View File

@ -1,5 +1,12 @@
# Changelog
## 2024-11-24 - 3.2.2 - fix(core)
Refactor Bucket class for improved error handling
- Ensured safe access using non-null assertions when finding a bucket.
- Enhanced fastPut method by adding fastPutStrict for safer operations.
- Added explicit error handling and type checking in fastExists method.
## 2024-11-24 - 3.2.1 - fix(metadata)
Fix metadata handling for deleted files

View File

@ -3,6 +3,6 @@
*/
export const commitinfo = {
name: '@push.rocks/smartbucket',
version: '3.2.1',
version: '3.2.2',
description: 'A TypeScript library offering simple and cloud-agnostic object storage with advanced features like bucket creation, file and directory management, and data streaming.'
}

View File

@ -17,7 +17,7 @@ export class Bucket {
public static async getBucketByName(smartbucketRef: SmartBucket, bucketNameArg: string) {
const command = new plugins.s3.ListBucketsCommand({});
const buckets = await smartbucketRef.s3Client.send(command);
const foundBucket = buckets.Buckets.find((bucket) => bucket.Name === bucketNameArg);
const foundBucket = buckets.Buckets!.find((bucket) => bucket.Name === bucketNameArg);
if (foundBucket) {
console.log(`bucket with name ${bucketNameArg} exists.`);
@ -88,14 +88,15 @@ export class Bucket {
contents: string | Buffer;
overwrite?: boolean;
}
): Promise<File> {
): Promise<File | null> {
try {
const reducedPath = await helpers.reducePathDescriptorToPath(optionsArg);
const exists = await this.fastExists({ path: reducedPath });
if (exists && !optionsArg.overwrite) {
console.error(`Object already exists at path '${reducedPath}' in bucket '${this.name}'.`);
return;
const errorText = `Object already exists at path '${reducedPath}' in bucket '${this.name}'.`;
console.error(errorText);
return null;
} else if (exists && optionsArg.overwrite) {
console.log(
`Overwriting existing object at path '${reducedPath}' in bucket '${this.name}'.`
@ -128,6 +129,14 @@ export class Bucket {
}
}
public async fastPutStrict(...args: Parameters<Bucket['fastPut']>) {
const file = await this.fastPut(...args);
if (!file) {
throw new Error(`File not stored at path '${args[0].path}'`);
}
return file;
}
/**
* get file
*/
@ -152,7 +161,7 @@ export class Bucket {
},
});
await done.promise;
return completeFile;
return completeFile!;
}
/**
@ -220,7 +229,7 @@ export class Bucket {
return chunk;
},
finalFunction: async (cb) => {
return null;
return null!;
},
});
@ -392,8 +401,8 @@ export class Bucket {
await this.smartbucketRef.s3Client.send(command);
console.log(`Object '${optionsArg.path}' exists in bucket '${this.name}'.`);
return true;
} catch (error) {
if (error.name === 'NotFound') {
} catch (error: any) {
if (error?.name === 'NotFound') {
console.log(`Object '${optionsArg.path}' does not exist in bucket '${this.name}'.`);
return false;
} else {