Skip to content

Commit

Permalink
CLEN-1556/fix/error handling when send file encounters issues (#338)
Browse files Browse the repository at this point in the history
* fix: error handling when send file encounters issues

* PubNub SDK v7.3.3 release.

---------

Co-authored-by: PubNub Release Bot <[email protected]>
  • Loading branch information
mohitpubnub and pubnub-release-bot authored Sep 11, 2023
1 parent 3745862 commit d1fca58
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 93 deletions.
11 changes: 8 additions & 3 deletions .pubnub.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
---
changelog:
- date: 2023-09-11
version: v7.3.3
changes:
- type: bug
text: "Fixes issue of getting misleading error message when sendFile fails."
- date: 2023-08-31
version: v7.3.2
changes:
Expand Down Expand Up @@ -890,7 +895,7 @@ supported-platforms:
- 'Ubuntu 14.04 and up'
- 'Windows 7 and up'
version: 'Pubnub Javascript for Node'
version: '7.3.2'
version: '7.3.3'
sdks:
- full-name: PubNub Javascript SDK
short-name: Javascript
Expand All @@ -906,7 +911,7 @@ sdks:
- distribution-type: source
distribution-repository: GitHub release
package-name: pubnub.js
location: https://github.com/pubnub/javascript/archive/refs/tags/v7.3.2.zip
location: https://github.com/pubnub/javascript/archive/refs/tags/v7.3.3.zip
requires:
- name: 'agentkeepalive'
min-version: '3.5.2'
Expand Down Expand Up @@ -1577,7 +1582,7 @@ sdks:
- distribution-type: library
distribution-repository: GitHub release
package-name: pubnub.js
location: https://github.com/pubnub/javascript/releases/download/v7.3.2/pubnub.7.3.2.js
location: https://github.com/pubnub/javascript/releases/download/v7.3.3/pubnub.7.3.3.js
requires:
- name: 'agentkeepalive'
min-version: '3.5.2'
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## v7.3.3
September 11 2023

#### Fixed
- Fixes issue of getting misleading error message when sendFile fails.

## v7.3.2
August 31 2023

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ Watch [Getting Started with PubNub JS SDK](https://app.dashcam.io/replay/64ee0d2
npm install pubnub
```
* or download one of our builds from our CDN:
* https://cdn.pubnub.com/sdk/javascript/pubnub.7.3.2.js
* https://cdn.pubnub.com/sdk/javascript/pubnub.7.3.2.min.js
* https://cdn.pubnub.com/sdk/javascript/pubnub.7.3.3.js
* https://cdn.pubnub.com/sdk/javascript/pubnub.7.3.3.min.js
2. Configure your keys:
Expand Down
55 changes: 22 additions & 33 deletions dist/web/pubnub.js
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,7 @@
return this;
};
default_1.prototype.getVersion = function () {
return '7.3.2';
return '7.3.3';
};
default_1.prototype._addPnsdkSuffix = function (name, suffix) {
this._PNSDKSuffix[name] = suffix;
Expand Down Expand Up @@ -4679,17 +4679,6 @@
}); },
};

var getErrorFromResponse = function (response) {
return new Promise(function (resolve) {
var result = '';
response.on('data', function (data) {
result += data.toString('utf8');
});
response.on('end', function () {
resolve(result);
});
});
};
var sendFile = function (_a) {
var _this = this;
var generateUploadUrl = _a.generateUploadUrl, publishFile = _a.publishFile, _b = _a.modules, PubNubFile = _b.PubNubFile, config = _b.config, cryptography = _b.cryptography, networking = _b.networking;
Expand Down Expand Up @@ -4726,7 +4715,7 @@
}
_s.label = 4;
case 4:
_s.trys.push([4, 18, , 22]);
_s.trys.push([4, 18, , 19]);
if (!(PubNubFile.supportsFileUri && input.uri)) return [3 /*break*/, 7];
_f = (_e = networking).POSTFILE;
_g = [url, formFieldsWithMimeType];
Expand Down Expand Up @@ -4763,27 +4752,27 @@
result = _s.sent();
return [3 /*break*/, 17];
case 16: throw new Error('Unsupported environment');
case 17: return [3 /*break*/, 22];
case 17: return [3 /*break*/, 19];
case 18:
e_1 = _s.sent();
if (!e_1.response) return [3 /*break*/, 20];
return [4 /*yield*/, getErrorFromResponse(e_1.response)];
if (e_1.response && typeof e_1.response.text === 'string') {
errorBody = e_1.response.text;
reason = /<Message>(.*)<\/Message>/gi.exec(errorBody);
throw new PubNubError(reason ? "Upload to bucket failed: ".concat(reason[1]) : 'Upload to bucket failed.', e_1);
}
else {
throw new PubNubError('Upload to bucket failed.', e_1);
}
case 19:
errorBody = _s.sent();
reason = /<Message>(.*)<\/Message>/gi.exec(errorBody);
throw new PubNubError(reason ? "Upload to bucket failed: ".concat(reason[1]) : 'Upload to bucket failed.', e_1);
case 20: throw new PubNubError('Upload to bucket failed.', e_1);
case 21: return [3 /*break*/, 22];
case 22:
if (result.status !== 204) {
throw new PubNubError('Upload to bucket was unsuccessful', result);
}
retries = config.fileUploadPublishRetryLimit;
wasSuccessful = false;
publishResult = { timetoken: '0' };
_s.label = 23;
case 23:
_s.trys.push([23, 25, , 26]);
_s.label = 20;
case 20:
_s.trys.push([20, 22, , 23]);
return [4 /*yield*/, publishFile({
channel: channel,
message: message,
Expand All @@ -4793,19 +4782,19 @@
storeInHistory: storeInHistory,
ttl: ttl,
})];
case 24:
case 21:
/* eslint-disable-next-line no-await-in-loop */
publishResult = _s.sent();
wasSuccessful = true;
return [3 /*break*/, 26];
case 25:
return [3 /*break*/, 23];
case 22:
_s.sent();
retries -= 1;
return [3 /*break*/, 26];
case 26:
if (!wasSuccessful && retries > 0) return [3 /*break*/, 23];
_s.label = 27;
case 27:
return [3 /*break*/, 23];
case 23:
if (!wasSuccessful && retries > 0) return [3 /*break*/, 20];
_s.label = 24;
case 24:
if (!wasSuccessful) {
throw new PubNubError('Publish failed. You may want to execute that operation manually using pubnub.publishFile', {
channel: channel,
Expand Down
2 changes: 1 addition & 1 deletion dist/web/pubnub.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/core/components/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ var default_1 = /** @class */ (function () {
return this;
};
default_1.prototype.getVersion = function () {
return '7.3.2';
return '7.3.3';
};
default_1.prototype._addPnsdkSuffix = function (name, suffix) {
this._PNSDKSuffix[name] = suffix;
Expand Down
54 changes: 22 additions & 32 deletions lib/core/endpoints/file_upload/send_file.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,6 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
};
Object.defineProperty(exports, "__esModule", { value: true });
var endpoint_1 = require("../../components/endpoint");
var getErrorFromResponse = function (response) {
return new Promise(function (resolve) {
var result = '';
response.on('data', function (data) {
result += data.toString('utf8');
});
response.on('end', function () {
resolve(result);
});
});
};
var sendFile = function (_a) {
var _this = this;
var generateUploadUrl = _a.generateUploadUrl, publishFile = _a.publishFile, _b = _a.modules, PubNubFile = _b.PubNubFile, config = _b.config, cryptography = _b.cryptography, networking = _b.networking;
Expand Down Expand Up @@ -84,7 +73,7 @@ var sendFile = function (_a) {
}
_s.label = 4;
case 4:
_s.trys.push([4, 18, , 22]);
_s.trys.push([4, 18, , 19]);
if (!(PubNubFile.supportsFileUri && input.uri)) return [3 /*break*/, 7];
_f = (_e = networking).POSTFILE;
_g = [url, formFieldsWithMimeType];
Expand Down Expand Up @@ -121,27 +110,28 @@ var sendFile = function (_a) {
result = _s.sent();
return [3 /*break*/, 17];
case 16: throw new Error('Unsupported environment');
case 17: return [3 /*break*/, 22];
case 17: return [3 /*break*/, 19];
case 18:
e_1 = _s.sent();
if (!e_1.response) return [3 /*break*/, 20];
return [4 /*yield*/, getErrorFromResponse(e_1.response)];
if (e_1.response && typeof e_1.response.text === 'string') {
errorBody = e_1.response.text;
reason = /<Message>(.*)<\/Message>/gi.exec(errorBody);
throw new endpoint_1.PubNubError(reason ? "Upload to bucket failed: ".concat(reason[1]) : 'Upload to bucket failed.', e_1);
}
else {
throw new endpoint_1.PubNubError('Upload to bucket failed.', e_1);
}
return [3 /*break*/, 19];
case 19:
errorBody = _s.sent();
reason = /<Message>(.*)<\/Message>/gi.exec(errorBody);
throw new endpoint_1.PubNubError(reason ? "Upload to bucket failed: ".concat(reason[1]) : 'Upload to bucket failed.', e_1);
case 20: throw new endpoint_1.PubNubError('Upload to bucket failed.', e_1);
case 21: return [3 /*break*/, 22];
case 22:
if (result.status !== 204) {
throw new endpoint_1.PubNubError('Upload to bucket was unsuccessful', result);
}
retries = config.fileUploadPublishRetryLimit;
wasSuccessful = false;
publishResult = { timetoken: '0' };
_s.label = 23;
case 23:
_s.trys.push([23, 25, , 26]);
_s.label = 20;
case 20:
_s.trys.push([20, 22, , 23]);
return [4 /*yield*/, publishFile({
channel: channel,
message: message,
Expand All @@ -151,19 +141,19 @@ var sendFile = function (_a) {
storeInHistory: storeInHistory,
ttl: ttl,
})];
case 24:
case 21:
/* eslint-disable-next-line no-await-in-loop */
publishResult = _s.sent();
wasSuccessful = true;
return [3 /*break*/, 26];
case 25:
return [3 /*break*/, 23];
case 22:
e_2 = _s.sent();
retries -= 1;
return [3 /*break*/, 26];
case 26:
if (!wasSuccessful && retries > 0) return [3 /*break*/, 23];
_s.label = 27;
case 27:
return [3 /*break*/, 23];
case 23:
if (!wasSuccessful && retries > 0) return [3 /*break*/, 20];
_s.label = 24;
case 24:
if (!wasSuccessful) {
throw new endpoint_1.PubNubError('Publish failed. You may want to execute that operation manually using pubnub.publishFile', {
channel: channel,
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.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pubnub",
"version": "7.3.2",
"version": "7.3.3",
"author": "PubNub <[email protected]>",
"description": "Publish & Subscribe Real-time Messaging with PubNub",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/core/components/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ export default class {
}

getVersion() {
return '7.3.2';
return '7.3.3';
}

_addPnsdkSuffix(name, suffix) {
Expand Down
19 changes: 2 additions & 17 deletions src/core/endpoints/file_upload/send_file.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
import { PubNubError, createValidationError } from '../../components/endpoint';

const getErrorFromResponse = (response) =>
new Promise((resolve) => {
let result = '';

response.on('data', (data) => {
result += data.toString('utf8');
});

response.on('end', () => {
resolve(result);
});
});

const sendFile = function ({
generateUploadUrl,
publishFile,
Expand Down Expand Up @@ -68,11 +55,9 @@ const sendFile = function ({
throw new Error('Unsupported environment');
}
} catch (e) {
if (e.response) {
const errorBody = await getErrorFromResponse(e.response);

if (e.response && typeof e.response.text === 'string') {
const errorBody = e.response.text;
const reason = /<Message>(.*)<\/Message>/gi.exec(errorBody);

throw new PubNubError(reason ? `Upload to bucket failed: ${reason[1]}` : 'Upload to bucket failed.', e);
} else {
throw new PubNubError('Upload to bucket failed.', e);
Expand Down

0 comments on commit d1fca58

Please sign in to comment.