0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-24 23:48:13 -05:00

Merge pull request #16 from TryGhost/deps-ember-ajax

deps: ember-ajax@2.4.1
This commit is contained in:
Austin Burdine 2016-06-06 09:39:18 -06:00
commit bec486f542
7 changed files with 74 additions and 41 deletions

View file

@ -1,8 +1,8 @@
import Ember from 'ember';
import { invoke, invokeAction } from 'ember-invoke-action';
import {
RequestEntityTooLargeError,
UnsupportedMediaTypeError
isRequestEntityTooLargeError,
isUnsupportedMediaTypeError
} from 'ghost-admin/services/ajax';
const {
@ -130,9 +130,9 @@ export default Component.extend({
_uploadFailed(error) {
let message;
if (error instanceof UnsupportedMediaTypeError) {
if (isUnsupportedMediaTypeError(error)) {
message = 'The file type you uploaded is not supported.';
} else if (error instanceof RequestEntityTooLargeError) {
} else if (isRequestEntityTooLargeError(error)) {
message = 'The file you uploaded was larger than the maximum file size your server allows.';
} else if (error.errors && !isBlank(error.errors[0].message)) {
message = error.errors[0].message;

View file

@ -1,6 +1,9 @@
import Ember from 'ember';
import ghostPaths from 'ghost-admin/utils/ghost-paths';
import {RequestEntityTooLargeError, UnsupportedMediaTypeError} from 'ghost-admin/services/ajax';
import {
isRequestEntityTooLargeError,
isUnsupportedMediaTypeError
} from 'ghost-admin/services/ajax';
const {
Component,
@ -143,9 +146,9 @@ export default Component.extend({
uploadFailed(error) {
let message;
if (error instanceof UnsupportedMediaTypeError) {
if (isUnsupportedMediaTypeError(error)) {
message = 'The image type you uploaded is not supported. Please use .PNG, .JPG, .GIF, .SVG.';
} else if (error instanceof RequestEntityTooLargeError) {
} else if (isRequestEntityTooLargeError(error)) {
message = 'The image you uploaded was larger than the maximum file size your server allows.';
} else if (error.errors && !isBlank(error.errors[0].message)) {
message = error.errors[0].message;

View file

@ -1,6 +1,6 @@
import Ember from 'ember';
import AjaxService from 'ember-ajax/services/ajax';
import {NotFoundError} from 'ghost-admin/services/ajax';
import {isNotFoundError} from 'ember-ajax/errors';
const {
Component,
@ -76,7 +76,7 @@ export default Component.extend({
.catch((error) => {
let defaultImageUrl = `url("${this.get('ghostPaths.subdir')}/ghost/img/user-image.png")`;
if (error instanceof NotFoundError) {
if (isNotFoundError(error)) {
this.$('.placeholder-img')[0].style.backgroundImage = Ember.String.htmlSafe(defaultImageUrl);
} else {
this.$('.placeholder-img')[0].style.backgroundImage = 'url()';

View file

@ -1,24 +1,41 @@
import Ember from 'ember';
import AjaxService from 'ember-ajax/services/ajax';
import {AjaxError} from 'ember-ajax/errors';
import {AjaxError, isAjaxError} from 'ember-ajax/errors';
import config from 'ghost-admin/config/environment';
const {inject, computed} = Ember;
const {
computed,
inject,
isArray
} = Ember;
export function RequestEntityTooLargeError(errors) {
AjaxError.call(this, errors, 'Request was rejected because it\'s larger than the maximum file size the server allows');
}
RequestEntityTooLargeError.prototype = Object.create(AjaxError.prototype);
export function isRequestEntityTooLargeError(error) {
if (isAjaxError(error)) {
return error instanceof RequestEntityTooLargeError;
} else {
return error === 413;
}
}
export function UnsupportedMediaTypeError(errors) {
AjaxError.call(this, errors, 'Request was rejected because it contains an unknown or unsupported file type.');
}
// TODO: remove once upgraded to ember-ajax 2.0
export function NotFoundError(errors) {
AjaxError.call(this, errors, 'Resource was not found.');
}
UnsupportedMediaTypeError.prototype = Object.create(AjaxError.prototype);
NotFoundError.prototype = Object.create(AjaxError.prototype);
export function isUnsupportedMediaTypeError(error) {
if (isAjaxError(error)) {
return error instanceof UnsupportedMediaTypeError;
} else {
return error === 415;
}
}
export default AjaxService.extend({
session: inject.service(),
@ -39,12 +56,10 @@ export default AjaxService.extend({
}),
handleResponse(status, headers, payload) {
if (this.isRequestEntityTooLarge(status, headers, payload)) {
if (this.isRequestEntityTooLargeError(status, headers, payload)) {
return new RequestEntityTooLargeError(payload.errors);
} else if (this.isUnsupportedMediaType(status, headers, payload)) {
} else if (this.isUnsupportedMediaTypeError(status, headers, payload)) {
return new UnsupportedMediaTypeError(payload.errors);
} else if (this.isNotFoundError(status, headers, payload)) {
return new NotFoundError(payload.errors);
}
return this._super(...arguments);
@ -53,20 +68,26 @@ export default AjaxService.extend({
normalizeErrorResponse(status, headers, payload) {
if (payload && typeof payload === 'object') {
payload.errors = payload.error || payload.errors || payload.message || undefined;
if (isArray(payload.errors)) {
payload.errors = payload.errors.map(function(error) {
if (typeof error === 'string') {
return {message: error};
} else {
return error;
}
});
}
}
return this._super(status, headers, payload);
},
isRequestEntityTooLarge(status/*, headers, payload */) {
return status === 413;
isRequestEntityTooLargeError(status/*, headers, payload */) {
return isRequestEntityTooLargeError(status);
},
isUnsupportedMediaType(status/*, headers, payload */) {
return status === 415;
},
isNotFoundError(status) {
return status === 404;
isUnsupportedMediaTypeError(status/*, headers, payload */) {
return isUnsupportedMediaTypeError(status);
}
});

View file

@ -1,5 +1,5 @@
import Ember from 'ember';
import {AjaxError} from 'ember-ajax/errors';
import {isAjaxError} from 'ember-ajax/errors';
const {
Service,
@ -115,7 +115,7 @@ export default Service.extend({
options.defaultErrorText = options.defaultErrorText || 'There was a problem on the server, please try again.';
if (resp instanceof AjaxError) {
if (isAjaxError(resp)) {
resp = resp.errors;
}

View file

@ -26,7 +26,7 @@
"devDependencies": {
"broccoli-asset-rev": "2.4.2",
"csscomb": "3.1.8",
"ember-ajax": "0.7.1",
"ember-ajax": "2.4.1",
"ember-cli": "2.5.1",
"ember-cli-app-version": "1.0.0",
"ember-cli-babel": "5.1.6",

View file

@ -4,8 +4,14 @@ import {
it
} from 'ember-mocha';
import Pretender from 'pretender';
import {AjaxError, UnauthorizedError} from 'ember-ajax/errors';
import {RequestEntityTooLargeError, UnsupportedMediaTypeError} from 'ghost-admin/services/ajax';
import {
isAjaxError,
isUnauthorizedError
} from 'ember-ajax/errors';
import {
isRequestEntityTooLargeError,
isUnsupportedMediaTypeError
} from 'ghost-admin/services/ajax';
import config from 'ghost-admin/config/environment';
function stubAjaxEndpoint(server, response = {}, code = 200) {
@ -85,7 +91,10 @@ describeModule(
ajax.request('/test/').then(() => {
expect(false).to.be.true();
}).catch((error) => {
expect(error.errors).to.deep.equal(['First Error', 'Second Error']);
expect(error.errors).to.deep.equal([
{message: 'First Error'},
{message: 'Second Error'}
]);
done();
});
});
@ -98,12 +107,12 @@ describeModule(
ajax.request('/test/').then(() => {
expect(false).to.be.true;
}).catch((error) => {
expect(error).to.be.instanceOf(AjaxError);
expect(isAjaxError(error)).to.be.true;
done();
});
});
it('returns known error object for built-in errors', function (done) {
it('handles error checking for built-in errors', function (done) {
stubAjaxEndpoint(server, '', 401);
let ajax = this.subject();
@ -111,12 +120,12 @@ describeModule(
ajax.request('/test/').then(() => {
expect(false).to.be.true;
}).catch((error) => {
expect(error).to.be.instanceOf(UnauthorizedError);
expect(isUnauthorizedError(error)).to.be.true;
done();
});
});
it('returns RequestEntityTooLargeError object for 413 errors', function (done) {
it('handles error checking for RequestEntityTooLargeError on 413 errors', function (done) {
stubAjaxEndpoint(server, {}, 413);
let ajax = this.subject();
@ -124,12 +133,12 @@ describeModule(
ajax.request('/test/').then(() => {
expect(false).to.be.true;
}).catch((error) => {
expect(error).to.be.instanceOf(RequestEntityTooLargeError);
expect(isRequestEntityTooLargeError(error)).to.be.true;
done();
});
});
it('returns UnsupportedMediaTypeError object for 415 errors', function (done) {
it('handles error checking for UnsupportedMediaTypeError on 415 errors', function (done) {
stubAjaxEndpoint(server, {}, 415);
let ajax = this.subject();
@ -137,7 +146,7 @@ describeModule(
ajax.request('/test/').then(() => {
expect(false).to.be.true;
}).catch((error) => {
expect(error).to.be.instanceOf(UnsupportedMediaTypeError);
expect(isUnsupportedMediaTypeError(error)).to.be.true;
done();
});
});