Просмотр исходного кода

Added error handler that displays error in toasting

Rafał Pitoń 10 лет назад
Родитель
Сommit
637dcc6476

+ 24 - 1
misago/emberapp/app/routes/application.js

@@ -23,7 +23,7 @@ export default Ember.Route.extend({
       return false;
       return false;
     },
     },
 
 
-    // Error handler
+    // Error handlers
 
 
     error: function(reason) {
     error: function(reason) {
       if (reason.status === 0) {
       if (reason.status === 0) {
@@ -56,6 +56,29 @@ export default Ember.Route.extend({
       return true;
       return true;
     },
     },
 
 
+    toastError: function(reason) {
+      var errorMessage = gettext('Unknown error has occured.');
+
+      if (reason.status === 0) {
+        errorMessage = gettext('Lost connection with application.');
+      }
+
+      if (reason.status === 403) {
+        if (typeof reason.responseJSON !== 'undefined' && typeof reason.responseJSON.detail !== 'undefined' && reason.responseJSON.detail !== 'Permission denied') {
+          errorMessage = reason.responseJSON.detail;
+        } else {
+          errorMessage = gettext("You don't have permission to perform this action.");
+        }
+      }
+
+      if (reason.status === 404) {
+        errorMessage = gettext('Action link is invalid.');
+      }
+
+      this.get('toast').error(errorMessage);
+      return false;
+    },
+
     showBan: function(ban) {
     showBan: function(ban) {
       this.send('setTitle', gettext('You are banned'));
       this.send('setTitle', gettext('You are banned'));
       this.intermediateTransitionTo('error-banned', ban);
       this.intermediateTransitionTo('error-banned', ban);

+ 20 - 6
misago/emberapp/tests/acceptance/error-handling-test.js → misago/emberapp/tests/acceptance/error-test.js

@@ -15,8 +15,10 @@ module('Acceptance: Application Error Handler', {
 });
 });
 
 
 test('some unhandled error occured', function(assert) {
 test('some unhandled error occured', function(assert) {
+  assert.expect(1);
+
   Ember.$.mockjax({
   Ember.$.mockjax({
-    url: "/api/legal-pages/privacy-policy/",
+    url: '/api/legal-pages/privacy-policy/',
     status: 500,
     status: 500,
     responseText: {
     responseText: {
       'detail': 'Some terrible Django error'
       'detail': 'Some terrible Django error'
@@ -31,8 +33,10 @@ test('some unhandled error occured', function(assert) {
 });
 });
 
 
 test('app went away', function(assert) {
 test('app went away', function(assert) {
+  assert.expect(1);
+
   Ember.$.mockjax({
   Ember.$.mockjax({
-    url: "/api/legal-pages/privacy-policy/",
+    url: '/api/legal-pages/privacy-policy/',
     status: 0,
     status: 0,
     responseText: {
     responseText: {
       'detail': 'Connection rejected'
       'detail': 'Connection rejected'
@@ -47,8 +51,10 @@ test('app went away', function(assert) {
 });
 });
 
 
 test('not found', function(assert) {
 test('not found', function(assert) {
+  assert.expect(1);
+
   Ember.$.mockjax({
   Ember.$.mockjax({
-    url: "/api/legal-pages/privacy-policy/",
+    url: '/api/legal-pages/privacy-policy/',
     status: 404,
     status: 404,
     responseText: {
     responseText: {
       'detail': 'Not found'
       'detail': 'Not found'
@@ -63,8 +69,10 @@ test('not found', function(assert) {
 });
 });
 
 
 test('permission denied', function(assert) {
 test('permission denied', function(assert) {
+  assert.expect(1);
+
   Ember.$.mockjax({
   Ember.$.mockjax({
-    url: "/api/legal-pages/privacy-policy/",
+    url: '/api/legal-pages/privacy-policy/',
     status: 403,
     status: 403,
     responseText: {
     responseText: {
       'detail': 'Permission denied'
       'detail': 'Permission denied'
@@ -79,8 +87,10 @@ test('permission denied', function(assert) {
 });
 });
 
 
 test('permission denied with reason', function(assert) {
 test('permission denied with reason', function(assert) {
+  assert.expect(2);
+
   Ember.$.mockjax({
   Ember.$.mockjax({
-    url: "/api/legal-pages/privacy-policy/",
+    url: '/api/legal-pages/privacy-policy/',
     status: 403,
     status: 403,
     responseText: {
     responseText: {
       'detail': 'Lorem ipsum dolor met.'
       'detail': 'Lorem ipsum dolor met.'
@@ -97,8 +107,10 @@ test('permission denied with reason', function(assert) {
 });
 });
 
 
 test('banned', function(assert) {
 test('banned', function(assert) {
+  assert.expect(3);
+
   Ember.$.mockjax({
   Ember.$.mockjax({
-    url: "/api/legal-pages/privacy-policy/",
+    url: '/api/legal-pages/privacy-policy/',
     status: 403,
     status: 403,
     responseText: {
     responseText: {
       'ban': {
       'ban': {
@@ -125,6 +137,8 @@ test('banned', function(assert) {
 });
 });
 
 
 test('not found route', function(assert) {
 test('not found route', function(assert) {
+  assert.expect(1);
+
   visit('/this-url-really-doesnt-exist');
   visit('/this-url-really-doesnt-exist');
 
 
   andThen(function() {
   andThen(function() {

+ 125 - 0
misago/emberapp/tests/acceptance/error-toast-test.js

@@ -0,0 +1,125 @@
+import Ember from 'ember';
+import { module, test } from 'qunit';
+import startApp from '../helpers/start-app';
+import getToastMessage from '../helpers/toast-message';
+
+var application;
+
+module('Acceptance: Application Error Handler', {
+  beforeEach: function() {
+    application = startApp();
+  },
+  afterEach: function() {
+    Ember.run(application, 'destroy');
+    Ember.$.mockjax.clear();
+  }
+});
+
+test('some unhandled error occured', function(assert) {
+  assert.expect(1);
+
+  Ember.$.mockjax({
+    url: '/api/auth/login/',
+    status: 500
+  });
+
+  visit('/');
+
+  click('.guest-nav .btn-login');
+  fillIn('#loginModal .form-group:first-child input', 'SomeFake');
+  fillIn('#loginModal .form-group:last-child input', 'pass1234');
+  click('#loginModal .btn-primary');
+
+  andThen(function() {
+    assert.equal(getToastMessage(), 'Unknown error has occured.');
+  });
+});
+
+test('app went away', function(assert) {
+  assert.expect(1);
+
+  Ember.$.mockjax({
+    url: '/api/auth/login/',
+    status: 0
+  });
+
+  visit('/');
+
+  click('.guest-nav .btn-login');
+  fillIn('#loginModal .form-group:first-child input', 'SomeFake');
+  fillIn('#loginModal .form-group:last-child input', 'pass1234');
+  click('#loginModal .btn-primary');
+
+  andThen(function() {
+    assert.equal(getToastMessage(), 'Lost connection with application.');
+  });
+});
+
+test('not found', function(assert) {
+  assert.expect(1);
+
+  Ember.$.mockjax({
+    url: '/api/auth/login/',
+    status: 404,
+    responseText: {
+      'detail': 'Not found'
+    }
+  });
+
+  visit('/');
+
+  click('.guest-nav .btn-login');
+  fillIn('#loginModal .form-group:first-child input', 'SomeFake');
+  fillIn('#loginModal .form-group:last-child input', 'pass1234');
+  click('#loginModal .btn-primary');
+
+  andThen(function() {
+    assert.equal(getToastMessage(), 'Action link is invalid.');
+  });
+});
+
+test('permission denied', function(assert) {
+  assert.expect(1);
+
+  Ember.$.mockjax({
+    url: '/api/auth/login/',
+    status: 403,
+    responseText: {
+      'detail': 'Permission denied'
+    }
+  });
+
+  visit('/');
+
+  click('.guest-nav .btn-login');
+  fillIn('#loginModal .form-group:first-child input', 'SomeFake');
+  fillIn('#loginModal .form-group:last-child input', 'pass1234');
+  click('#loginModal .btn-primary');
+
+  andThen(function() {
+    assert.equal(getToastMessage(), "You don't have permission to perform this action.");
+  });
+});
+
+test('permission denied with reason', function(assert) {
+  assert.expect(1);
+
+  Ember.$.mockjax({
+    url: '/api/auth/login/',
+    status: 403,
+    responseText: {
+      'detail': 'Lorem ipsum dolor met.'
+    }
+  });
+
+  visit('/');
+
+  click('.guest-nav .btn-login');
+  fillIn('#loginModal .form-group:first-child input', 'SomeFake');
+  fillIn('#loginModal .form-group:last-child input', 'pass1234');
+  click('#loginModal .btn-primary');
+
+  andThen(function() {
+    assert.equal(getToastMessage(), 'Lorem ipsum dolor met.');
+  });
+});