Skip to content

Commit be0c8a6

Browse files
authored
feat: Allow overriding Parse.Error message with custom message via new Core Manager option PARSE_ERRORS (#2014)
1 parent 6060792 commit be0c8a6

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

src/CoreManager.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ const config: Config & { [key: string]: mixed } = {
194194
ENCRYPTED_USER: false,
195195
IDEMPOTENCY: false,
196196
ALLOW_CUSTOM_OBJECT_ID: false,
197+
PARSE_ERRORS: [],
197198
};
198199

199200
function requireMethods(name: string, methods: Array<string>, controller: any) {

src/EventuallyQueue.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
import CoreManager from './CoreManager';
8+
import ParseError from './ParseError';
89
import ParseObject from './ParseObject';
910
import ParseQuery from './ParseQuery';
1011
import Storage from './Storage';
@@ -275,7 +276,7 @@ const EventuallyQueue = {
275276
await object.save(queueObject.object, queueObject.serverOptions);
276277
await this.remove(queueObject.queueId);
277278
} catch (e) {
278-
if (e.message !== 'XMLHttpRequest failed: "Unable to connect to the Parse API"') {
279+
if (e.code !== ParseError.CONNECTION_FAILED) {
279280
await this.remove(queueObject.queueId);
280281
}
281282
}
@@ -285,7 +286,7 @@ const EventuallyQueue = {
285286
await object.destroy(queueObject.serverOptions);
286287
await this.remove(queueObject.queueId);
287288
} catch (e) {
288-
if (e.message !== 'XMLHttpRequest failed: "Unable to connect to the Parse API"') {
289+
if (e.code !== ParseError.CONNECTION_FAILED) {
289290
await this.remove(queueObject.queueId);
290291
}
291292
}

src/ParseError.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
import CoreManager from './CoreManager';
2+
13
/**
24
* Constructs a new Parse.Error object with the given code and message.
35
*
6+
* Parse.CoreManager.set('PARSE_ERRORS', [{ code, message }]) can be use to override error messages.
7+
*
48
* @alias Parse.Error
59
*/
610
class ParseError extends Error {
@@ -11,9 +15,15 @@ class ParseError extends Error {
1115
constructor(code, message) {
1216
super(message);
1317
this.code = code;
18+
let customMessage = message;
19+
CoreManager.get('PARSE_ERRORS').forEach((error) => {
20+
if (error.code === code && error.code) {
21+
customMessage = error.message;
22+
}
23+
});
1424
Object.defineProperty(this, 'message', {
1525
enumerable: true,
16-
value: message,
26+
value: customMessage,
1727
});
1828
}
1929

src/__tests__/ParseError-test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
jest.dontMock('../ParseError');
2+
jest.dontMock('../CoreManager');
23

34
const ParseError = require('../ParseError').default;
5+
const CoreManager = require('../CoreManager');
46

57
describe('ParseError', () => {
68
it('have sensible string representation', () => {
@@ -18,4 +20,14 @@ describe('ParseError', () => {
1820
code: 123,
1921
});
2022
});
23+
24+
it('can override message', () => {
25+
CoreManager.set('PARSE_ERRORS', [{ code: 123, message: 'Oops.' }]);
26+
const error = new ParseError(123, 'some error message');
27+
expect(JSON.parse(JSON.stringify(error))).toEqual({
28+
message: 'Oops.',
29+
code: 123,
30+
});
31+
CoreManager.set('PARSE_ERRORS', []);
32+
});
2133
});

0 commit comments

Comments
 (0)