Desktop: API: Improved error handling on service end-point

pull/3391/head
Laurent Cozic 2020-06-20 12:34:05 +01:00
parent 07720ed6f8
commit d27b658392
5 changed files with 45 additions and 34 deletions

View File

@ -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

1
.gitignore vendored
View File

@ -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

View File

@ -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);
},
};

View File

@ -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) {

View File

@ -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);
}
}