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: Allow overriding Parse.Error message with custom message via new Core Manager option PARSE_ERRORS #2014

Merged
merged 2 commits into from
Sep 1, 2023
Merged
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
1 change: 1 addition & 0 deletions src/CoreManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ const config: Config & { [key: string]: mixed } = {
ENCRYPTED_USER: false,
IDEMPOTENCY: false,
ALLOW_CUSTOM_OBJECT_ID: false,
PARSE_ERRORS: [],
};

function requireMethods(name: string, methods: Array<string>, controller: any) {
Expand Down
5 changes: 3 additions & 2 deletions src/EventuallyQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import CoreManager from './CoreManager';
import ParseError from './ParseError';
import ParseObject from './ParseObject';
import ParseQuery from './ParseQuery';
import Storage from './Storage';
Expand Down Expand Up @@ -275,7 +276,7 @@ const EventuallyQueue = {
await object.save(queueObject.object, queueObject.serverOptions);
await this.remove(queueObject.queueId);
} catch (e) {
if (e.message !== 'XMLHttpRequest failed: "Unable to connect to the Parse API"') {
if (e.code !== ParseError.CONNECTION_FAILED) {
await this.remove(queueObject.queueId);
}
}
Expand All @@ -285,7 +286,7 @@ const EventuallyQueue = {
await object.destroy(queueObject.serverOptions);
await this.remove(queueObject.queueId);
} catch (e) {
if (e.message !== 'XMLHttpRequest failed: "Unable to connect to the Parse API"') {
if (e.code !== ParseError.CONNECTION_FAILED) {
await this.remove(queueObject.queueId);
}
}
Expand Down
12 changes: 11 additions & 1 deletion src/ParseError.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import CoreManager from './CoreManager';

/**
* Constructs a new Parse.Error object with the given code and message.
*
* Parse.CoreManager.set('PARSE_ERRORS', [{ code, message }]) can be use to override error messages.
*
* @alias Parse.Error
*/
class ParseError extends Error {
Expand All @@ -11,9 +15,15 @@ class ParseError extends Error {
constructor(code, message) {
super(message);
this.code = code;
let customMessage = message;
CoreManager.get('PARSE_ERRORS').forEach((error) => {
if (error.code === code && error.code) {
customMessage = error.message;
}
});
Object.defineProperty(this, 'message', {
enumerable: true,
value: message,
value: customMessage,
});
}

Expand Down
12 changes: 12 additions & 0 deletions src/__tests__/ParseError-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
jest.dontMock('../ParseError');
jest.dontMock('../CoreManager');

const ParseError = require('../ParseError').default;
const CoreManager = require('../CoreManager');

describe('ParseError', () => {
it('have sensible string representation', () => {
Expand All @@ -18,4 +20,14 @@ describe('ParseError', () => {
code: 123,
});
});

it('can override message', () => {
CoreManager.set('PARSE_ERRORS', [{ code: 123, message: 'Oops.' }]);
const error = new ParseError(123, 'some error message');
expect(JSON.parse(JSON.stringify(error))).toEqual({
message: 'Oops.',
code: 123,
});
CoreManager.set('PARSE_ERRORS', []);
});
});