From 71d9c842ab17c2d8a7b56c40bceaf260caf6a7ee Mon Sep 17 00:00:00 2001 From: Diamond Lewis Date: Fri, 1 Sep 2023 10:49:14 -0500 Subject: [PATCH 1/2] feat: Allow for custom `Parse.Error` messages --- src/CoreManager.js | 1 + src/ParseError.js | 12 +++++++++++- src/__tests__/ParseError-test.js | 22 ++++++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/CoreManager.js b/src/CoreManager.js index 73228c6d3..dd38872d8 100644 --- a/src/CoreManager.js +++ b/src/CoreManager.js @@ -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, controller: any) { diff --git a/src/ParseError.js b/src/ParseError.js index 6383a5fb2..df89cc137 100644 --- a/src/ParseError.js +++ b/src/ParseError.js @@ -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 for error codes except CONNECTION_FAILED (100). + * * @alias Parse.Error */ class ParseError extends Error { @@ -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 !== ParseError.CONNECTION_FAILED) { + customMessage = error.message; + } + }); Object.defineProperty(this, 'message', { enumerable: true, - value: message, + value: customMessage, }); } diff --git a/src/__tests__/ParseError-test.js b/src/__tests__/ParseError-test.js index a74b6d568..f7f844b92 100644 --- a/src/__tests__/ParseError-test.js +++ b/src/__tests__/ParseError-test.js @@ -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', () => { @@ -18,4 +20,24 @@ 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', []); + }); + + it('cannot override connection failed message', () => { + CoreManager.set('PARSE_ERRORS', [{ code: 100, message: 'Cannot connect to server' }]); + const error = new ParseError(100, 'some error message'); + expect(JSON.parse(JSON.stringify(error))).toEqual({ + message: 'some error message', + code: 100, + }); + CoreManager.set('PARSE_ERRORS', []); + }); }); From 3e4dcc0817e0feddd56f7834db5b4d3ff5037ef6 Mon Sep 17 00:00:00 2001 From: Diamond Lewis Date: Fri, 1 Sep 2023 11:01:08 -0500 Subject: [PATCH 2/2] documentation --- src/EventuallyQueue.js | 5 +++-- src/ParseError.js | 4 ++-- src/__tests__/ParseError-test.js | 10 ---------- 3 files changed, 5 insertions(+), 14 deletions(-) diff --git a/src/EventuallyQueue.js b/src/EventuallyQueue.js index 03dc82ce8..88df1d3bd 100644 --- a/src/EventuallyQueue.js +++ b/src/EventuallyQueue.js @@ -5,6 +5,7 @@ */ import CoreManager from './CoreManager'; +import ParseError from './ParseError'; import ParseObject from './ParseObject'; import ParseQuery from './ParseQuery'; import Storage from './Storage'; @@ -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); } } @@ -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); } } diff --git a/src/ParseError.js b/src/ParseError.js index df89cc137..68c97501e 100644 --- a/src/ParseError.js +++ b/src/ParseError.js @@ -3,7 +3,7 @@ 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 for error codes except CONNECTION_FAILED (100). + * Parse.CoreManager.set('PARSE_ERRORS', [{ code, message }]) can be use to override error messages. * * @alias Parse.Error */ @@ -17,7 +17,7 @@ class ParseError extends Error { this.code = code; let customMessage = message; CoreManager.get('PARSE_ERRORS').forEach((error) => { - if (error.code === code && error.code !== ParseError.CONNECTION_FAILED) { + if (error.code === code && error.code) { customMessage = error.message; } }); diff --git a/src/__tests__/ParseError-test.js b/src/__tests__/ParseError-test.js index f7f844b92..5bbb5f81c 100644 --- a/src/__tests__/ParseError-test.js +++ b/src/__tests__/ParseError-test.js @@ -30,14 +30,4 @@ describe('ParseError', () => { }); CoreManager.set('PARSE_ERRORS', []); }); - - it('cannot override connection failed message', () => { - CoreManager.set('PARSE_ERRORS', [{ code: 100, message: 'Cannot connect to server' }]); - const error = new ParseError(100, 'some error message'); - expect(JSON.parse(JSON.stringify(error))).toEqual({ - message: 'some error message', - code: 100, - }); - CoreManager.set('PARSE_ERRORS', []); - }); });