diff --git a/.eslintignore b/.eslintignore index 3df656a84..96158845f 100644 --- a/.eslintignore +++ b/.eslintignore @@ -115,6 +115,7 @@ ReactNativeClient/lib/services/keychain/KeychainServiceDriver.node.js ReactNativeClient/lib/services/keychain/KeychainServiceDriverBase.js ReactNativeClient/lib/services/ResourceEditWatcher.js ReactNativeClient/lib/services/rest/actionApi.desktop.js +ReactNativeClient/lib/services/rest/errors.js ReactNativeClient/lib/services/SettingUtils.js ReactNativeClient/lib/services/UndoRedoService.js ReactNativeClient/lib/ShareExtension.js diff --git a/.gitignore b/.gitignore index 6b2b19d1d..d95cdf5c9 100644 --- a/.gitignore +++ b/.gitignore @@ -105,6 +105,7 @@ ReactNativeClient/lib/services/keychain/KeychainServiceDriver.node.js ReactNativeClient/lib/services/keychain/KeychainServiceDriverBase.js ReactNativeClient/lib/services/ResourceEditWatcher.js ReactNativeClient/lib/services/rest/actionApi.desktop.js +ReactNativeClient/lib/services/rest/errors.js ReactNativeClient/lib/services/SettingUtils.js ReactNativeClient/lib/services/UndoRedoService.js ReactNativeClient/lib/ShareExtension.js diff --git a/ReactNativeClient/lib/services/ExternalEditWatcher.js b/ReactNativeClient/lib/services/ExternalEditWatcher.js index ccea96d8b..c7d81d1b3 100644 --- a/ReactNativeClient/lib/services/ExternalEditWatcher.js +++ b/ReactNativeClient/lib/services/ExternalEditWatcher.js @@ -9,6 +9,7 @@ const spawn = require('child_process').spawn; const chokidar = require('chokidar'); const { bridge } = require('electron').remote.require('./bridge'); const { time } = require('lib/time-utils.js'); +const { ErrorNotFound } = require('./rest/errors'); class ExternalEditWatcher { constructor() { @@ -27,16 +28,22 @@ class ExternalEditWatcher { } externalApi() { + const loadNote = async (noteId) => { + const note = await Note.load(noteId); + if (!note) throw new ErrorNotFound(`No such note: ${noteId}`); + return note; + }; + return { openAndWatch: async ({ noteId }) => { - const note = await Note.load(noteId); + const note = await loadNote(noteId); return this.openAndWatch(note); }, stopWatching: async ({ noteId }) => { return this.stopWatching(noteId); }, noteIsWatched: async ({ noteId }) => { - const note = await Note.load(noteId); + const note = await loadNote(noteId); return this.noteIsWatched(note); }, }; diff --git a/ReactNativeClient/lib/services/rest/Api.js b/ReactNativeClient/lib/services/rest/Api.js index 4b93b90dd..e315c2168 100644 --- a/ReactNativeClient/lib/services/rest/Api.js +++ b/ReactNativeClient/lib/services/rest/Api.js @@ -24,38 +24,7 @@ const { FoldersScreenUtils } = require('lib/folders-screen-utils.js'); const uri2path = require('file-uri-to-path'); const { MarkupToHtml } = require('lib/joplin-renderer'); const { uuid } = require('lib/uuid'); - -class ApiError extends Error { - constructor(message, httpCode = 400) { - super(message); - this.httpCode_ = httpCode; - } - - get httpCode() { - return this.httpCode_; - } -} - -class ErrorMethodNotAllowed extends ApiError { - constructor(message = 'Method Not Allowed') { - super(message, 405); - } -} -class ErrorNotFound extends ApiError { - constructor(message = 'Not Found') { - super(message, 404); - } -} -class ErrorForbidden extends ApiError { - constructor(message = 'Forbidden') { - super(message, 403); - } -} -class ErrorBadRequest extends ApiError { - constructor(message = 'Bad Request') { - super(message, 400); - } -} +const { ErrorMethodNotAllowed, ErrorForbidden, ErrorBadRequest, ErrorNotFound } = require('./errors'); class Api { constructor(token = null, actionApi = null) { diff --git a/ReactNativeClient/lib/services/rest/errors.ts b/ReactNativeClient/lib/services/rest/errors.ts new file mode 100644 index 000000000..321ab792c --- /dev/null +++ b/ReactNativeClient/lib/services/rest/errors.ts @@ -0,0 +1,33 @@ +class ApiError extends Error { + private httpCode_:number; + + constructor(message:string, httpCode:number = 400) { + super(message); + this.httpCode_ = httpCode; + } + + get httpCode() { + return this.httpCode_; + } +} + +export class ErrorMethodNotAllowed extends ApiError { + constructor(message = 'Method Not Allowed') { + super(message, 405); + } +} +export class ErrorNotFound extends ApiError { + constructor(message = 'Not Found') { + super(message, 404); + } +} +export class ErrorForbidden extends ApiError { + constructor(message = 'Forbidden') { + super(message, 403); + } +} +export class ErrorBadRequest extends ApiError { + constructor(message = 'Bad Request') { + super(message, 400); + } +}