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

feat: add nextAppendPosition #22

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 5 additions & 0 deletions src/OSSBaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,11 @@ export abstract class OSSBaseClient {
hostId = info.HostId;
}
err = new OSSClientError(status, info?.Code ?? 'Unknown', message, requestId, hostId);

// https://help.aliyun.com/zh/oss/support/http-status-code-409#section-rmc-hvd-j38
if (info?.Code === 'PositionNotEqualToLength' && result.headers['x-oss-next-append-position']) {
(err as any).nextAppendPosition = result.headers['x-oss-next-append-position'];
}
}

debug('generate error %o', err);
Expand Down
1 change: 1 addition & 0 deletions test/OSSObject.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ describe('test/OSSObject.test.ts', () => {
assert.equal(err.name, 'OSSClientError');
assert.equal(err.code, 'PositionNotEqualToLength');
assert.equal(err.status, 409);
assert.equal((err as any).nextAppendPosition, '3');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Enhance type safety by updating OSSClientError to include nextAppendPosition property.

Currently, nextAppendPosition is accessed using a type assertion (err as any), which bypasses TypeScript's type checking and may lead to runtime errors. To improve type safety and code clarity, consider adding the nextAppendPosition property to the OSSClientError class definition.

You can update the OSSClientError class as follows:

// In src/error/OSSClientError.ts
export class OSSClientError extends Error {
  code: string;
  status: number;
  // Existing properties...

  // Add the nextAppendPosition property
  nextAppendPosition?: string;

  // Constructor and other methods...
}

After updating the class, you can remove the type assertion in your test:

- assert.equal((err as any).nextAppendPosition, '3');
+ assert.equal(err.nextAppendPosition, '3');

assert.match(err.message, /Position is not equal to file length/);
return true;
});
Expand Down
Loading