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

CLEN-1556/fix/error handling when send file encounters issues #338

Merged
merged 2 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
53 changes: 21 additions & 32 deletions dist/web/pubnub.js
Original file line number Diff line number Diff line change
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.

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.

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
Loading