Skip to content
Open
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
4 changes: 3 additions & 1 deletion dist/web/pubnub.js
Original file line number Diff line number Diff line change
Expand Up @@ -3118,6 +3118,8 @@
data !== null && data !== void 0 ? data : (data = response.body);
if (status === 402)
message = 'Not available for used key set. Contact support@pubnub.com';
else if (status === 404)
message = 'Resource not found';
else if (status === 400) {
category = StatusCategory$1.PNBadRequestCategory;
message = 'Bad request';
Expand Down Expand Up @@ -5790,7 +5792,7 @@
activeCancellation = attemptCancellation;
const responseHandler = (res, error) => {
const retriableError = error ? error.category !== StatusCategory$1.PNCancelledCategory : true;
const retriableStatusCode = !res || res.status >= 400;
const retriableStatusCode = (!res || res.status >= 400) && (error === null || error === void 0 ? void 0 : error.statusCode) !== 404;
let delay = -1;
if (retriableError &&
retriableStatusCode &&
Expand Down
4 changes: 2 additions & 2 deletions dist/web/pubnub.min.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions lib/errors/pubnub-api-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ class PubNubAPIError extends Error {
data !== null && data !== void 0 ? data : (data = response.body);
if (status === 402)
message = 'Not available for used key set. Contact support@pubnub.com';
else if (status === 404)
message = 'Resource not found';
else if (status === 400) {
category = categories_1.default.PNBadRequestCategory;
message = 'Bad request';
Expand Down
2 changes: 1 addition & 1 deletion lib/transport/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class PubNubMiddleware {
activeCancellation = attemptCancellation;
const responseHandler = (res, error) => {
const retriableError = error ? error.category !== categories_1.default.PNCancelledCategory : true;
const retriableStatusCode = !res || res.status >= 400;
const retriableStatusCode = (!res || res.status >= 400) && (error === null || error === void 0 ? void 0 : error.statusCode) !== 404;
let delay = -1;
if (retriableError &&
retriableStatusCode &&
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/errors/pubnub-api-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export class PubNubAPIError extends Error {
data ??= response.body;

if (status === 402) message = 'Not available for used key set. Contact support@pubnub.com';
else if (status === 404) message = 'Resource not found';
else if (status === 400) {
category = StatusCategory.PNBadRequestCategory;
message = 'Bad request';
Expand Down
2 changes: 1 addition & 1 deletion src/transport/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export class PubNubMiddleware implements Transport {

const responseHandler = (res?: TransportResponse, error?: PubNubAPIError) => {
const retriableError = error ? error.category !== StatusCategory.PNCancelledCategory : true;
const retriableStatusCode = !res || res.status >= 400;
const retriableStatusCode = (!res || res.status >= 400) && error?.statusCode !== 404;
let delay = -1;

if (
Expand Down
59 changes: 59 additions & 0 deletions test/integration/endpoints/objects/channel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import nock from 'nock';
import { asResponse, allChannels, channel1 } from './fixtures.js';
import PubNub from '../../../../src/node/index';
import utils from '../../../utils';
import { RetryPolicy } from '../../../../src/core/components/retry-policy.js';

describe('objects channel', () => {
const PUBLISH_KEY = 'myPublishKey';
Expand Down Expand Up @@ -727,4 +728,62 @@ describe('objects channel', () => {
expect(lastResult.data).to.have.length.lessThan(10);
});
});
describe('retry policy', () => {
it('should not retry on 404 Not Found error with linear retry policy', async () => {
const channelName = 'non-existent-channel';

// Set up nock mock before creating PubNub instance
const scope = utils
.createNock()
.get(`/v2/objects/${SUBSCRIBE_KEY}/channels/${channelName}`)
.once()
.query(true)
.reply(404, {
status: 404,
error: {
message: 'Requested object was not found.',
source: 'objects',
},
});

// Create a new PubNub instance with linear retry policy
const pubnubWithRetry = new PubNub({
subscribeKey: SUBSCRIBE_KEY,
publishKey: PUBLISH_KEY,
uuid: UUID,
// @ts-expect-error Force override default value.
useRequestId: false,
authKey: AUTH_KEY,
retryConfiguration: RetryPolicy.LinearRetryPolicy({
delay: 2,
maximumRetry: 2,
}),
});

let caughtError: any;
try {
await pubnubWithRetry.objects.getChannelMetadata({ channel: channelName });
} catch (error) {
caughtError = error;
}

// Verify that an error was thrown
expect(caughtError).to.exist;

// Verify the error status code is 404
expect(caughtError).to.have.property('status');
expect(caughtError.status.statusCode).to.equal(404);

// Verify the error message
expect(caughtError.status.errorData).to.exist;
expect(caughtError.status.errorData.error).to.exist;
expect(caughtError.status.errorData.error.message).to.equal('Requested object was not found.');
expect(caughtError.status.errorData.error.source).to.equal('objects');

// Verify the scope was called exactly once (no retries on 404
expect(scope.isDone()).to.be.true;

pubnubWithRetry.destroy(true);
});
});
});