Ghost/ghost/admin/app/components/modal-import-content.js
Hannah Wolfe e4dbf9b0fd Updated error fallbacks to generic error message
refs: https://github.com/TryGhost/Team/issues/1121

- We've decided on one  preferred message for unexpected errors
- We want to use this everywhere where we don't know what to display
- We now have a GENERIC_ERROR_MESSAGE constant that we should use
2022-11-23 21:49:40 +00:00

106 lines
2.8 KiB
JavaScript

import ModalComponent from 'ghost-admin/components/modal-base';
import ghostPaths from 'ghost-admin/utils/ghost-paths';
import {GENERIC_ERROR_MESSAGE} from 'ghost-admin/services/notifications';
import {computed} from '@ember/object';
import {inject} from 'ghost-admin/decorators/inject';
import {
isRequestEntityTooLargeError,
isUnsupportedMediaTypeError,
isVersionMismatchError
} from 'ghost-admin/services/ajax';
import {inject as service} from '@ember/service';
export default ModalComponent.extend({
ajax: service(),
notifications: service(),
store: service(),
state: 'INIT',
file: null,
paramName: 'importfile',
importResponse: null,
errorMessage: null,
errorHeader: null,
showTryAgainButton: true,
// Allowed actions
confirm: () => {},
config: inject(),
uploadUrl: computed(function () {
return `${ghostPaths().apiRoot}/db`;
}),
formData: computed('file', function () {
let formData = new FormData();
formData.append(this.paramName, this.file);
return formData;
}),
actions: {
setFile(file) {
this.set('file', file);
this.generateRequest();
},
reset() {
this.set('errorMessage', null);
this.set('errorHeader', null);
this.set('file', null);
this.set('state', 'INIT');
this.set('showTryAgainButton', true);
},
closeModal() {
if (this.state !== 'UPLOADING') {
this._super(...arguments);
}
},
// noop - we don't want the enter key doing anything
confirm() {}
},
generateRequest() {
let ajax = this.ajax;
let formData = this.formData;
let url = this.uploadUrl;
this.set('state', 'UPLOADING');
ajax.post(url, {
data: formData,
processData: false,
contentType: false,
dataType: 'text'
}).then(() => {
this.set('state', 'PROCESSING');
}).catch((error) => {
this._uploadError(error);
this.set('state', 'ERROR');
});
},
_uploadError(error) {
let message;
let header = 'Import error';
if (isVersionMismatchError(error)) {
this.notifications.showAPIError(error);
}
if (isUnsupportedMediaTypeError(error)) {
message = 'The file type you uploaded is not supported.';
} else if (isRequestEntityTooLargeError(error)) {
message = 'The file you uploaded was larger than the maximum file size your server allows.';
} else {
console.error(error); // eslint-disable-line
message = GENERIC_ERROR_MESSAGE;
}
this.set('errorMessage', message);
this.set('errorHeader', header);
}
});