From d9ee9c43d3d4379056830db9c29a8d4ed80a60ba Mon Sep 17 00:00:00 2001 From: Jeroen van Oorschot Date: Mon, 15 Jul 2019 22:04:02 +0200 Subject: [PATCH] Allow server side error messages. --- README.md | 3 ++- src/js/easymde.js | 6 ++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 1b0b853..72d9a94 100644 --- a/README.md +++ b/README.md @@ -154,7 +154,8 @@ easyMDE.value('New input for **EasyMDE**'); - **imageAccept**: A comma-separated list of mime-types used to check image type before upload (note: never trust client, always check file types at server-side). Defaults to `image/png, image/jpeg`. - **imageUploadEndpoint**: The endpoint where the images data will be sent, via an asynchronous *POST* request. The server is supposed to save this image, and return a json response. - if the request was successfully processed (HTTP 200-OK): `{"data": {"filePath": ""}}` where *filePath* is the relative path of the image; - - otherwise: `{"error": ""}`, where *errorCode* can be `noFileGiven` (HTTP 400), `typeNotAllowed` (HTTP 415), `fileTooLarge` (HTTP 413) or `importError` (see *errorMessages* below). No default value. + - otherwise: `{"error": ""}`, where *errorCode* can be `noFileGiven` (HTTP 400), `typeNotAllowed` (HTTP 415), `fileTooLarge` (HTTP 413) or `importError` (see *errorMessages* below). If *errorCode* is not one of the *errorMessages*, it is alerted unchanged to the user. This allows for server side error messages. + No default value. - **imageCSRFToken**: CSRF token to include with AJAX call to upload image. For instance used with Django backend. - **imageTexts**: Texts displayed to the user (mainly on the status bar) for the import image feature, where `#image_name#`, `#image_size#` and `#image_max_size#` will replaced by their respective values, that can be used for customization or internationalization: - **sbInit**: Status message displayed initially if `uploadImage` is set to `true`. Defaults to `Attach files by drag and dropping or pasting from clipboard.`. diff --git a/src/js/easymde.js b/src/js/easymde.js index f2a210f..1b0b8b3 100644 --- a/src/js/easymde.js +++ b/src/js/easymde.js @@ -2020,9 +2020,11 @@ EasyMDE.prototype.uploadImage = function(file, onSuccess, onError) { if(this.status === 200 && response && !response.error && response.data && response.data.filePath) { onSuccess(window.location.origin + '/' + response.data.filePath); } else { - if(response.error && response.error in self.options.errorMessages) { + if(response.error && response.error in self.options.errorMessages) { // preformatted error message onError(fillErrorMessage(self.options.errorMessages[response.error])); - } else { + }else if(response.error){ // server side generated error message + onError(fillErrorMessage(response.error)); + } else { //unknown error console.log('EasyMDE: Received an unexpected response after uploading the image.' + this.status + ' (' + this.statusText + ')'); onError(fillErrorMessage(self.options.errorMessages.importError));