Errors.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. Ext.define('saas.util.Errors', {
  2. requires: ['Ext.window.Toast'],
  3. statics: {
  4. toForm: function(errors) {
  5. var values = {};
  6. if (Ext.isObject(errors)) {
  7. errors = errors.errors;
  8. };
  9. if (Ext.isArray(errors)) {
  10. errors.forEach(function(error) {
  11. var name = error.id || error.field || error.path;
  12. var value = error.msg || error.message;
  13. if (name && value) {
  14. values[name] = value;
  15. }
  16. });
  17. } else {
  18. values = errors;
  19. }
  20. return values;
  21. },
  22. process: function(error, form) {
  23. if (!error) {
  24. return false;
  25. }
  26. if (Ext.isFunction(error.hasException)) {
  27. // The given error is an Ext.data.operation.Operation
  28. if (!error.hasException()) {
  29. return false;
  30. }
  31. error = error.getError() || 'An unknown error has occurred';
  32. }
  33. if (Ext.isObject(error)) {
  34. if (error.code === -32096) { // READONLY_SESSION
  35. // The session is read-only (demo version), let's display a temporary message
  36. // and return false since this exception should not be considered as an error.
  37. saas.util.BaseUtil.showErrorToast(error.message);
  38. return false;
  39. }
  40. if (error.code === -32001 && form) {
  41. form.setErrors(this.toForm(error));
  42. } else {
  43. saas.util.BaseUtil.showErrorToast(error.name + ' 错误信息:' + error.message);
  44. }
  45. } else if (Ext.isString(error)) {
  46. saas.util.BaseUtil.showErrorToast('错误信息:' + error);
  47. }
  48. return true;
  49. }
  50. }
  51. });