How to create custom error classes in Ember?

What is the correct way to create custom error classes in Ember and where to put error class definition files in the Ember CLI?

All the code samples that I found fiddled with prototypes of JavaScript objects. Why can't I just call Ember.Error.extend, as we do for regular Ember objects?

The correct place for custom error classes should be in the app / errors / directory, but it seems that the Ember CLI does not resolve these files.

+4
source share
1 answer

Create your own file, for example, in a directory app/errors/and name it custom-error.js.

, :

import Ember from 'ember';

let CustomError = function (errors, message = 'This error is result of my custom logic.') {
  Ember.Error.call(this, message);

  this.errors = errors || [
    {
      title: 'This is custom error.',
      detail: message
    }
  ];
}

CustomError.prototype = Object.create(Ember.Error.prototype);

export default CustomError;

, -:

import Ember from 'ember';
import CustomError from '../errors/custom-error';

export default Ember.Controller.extend({
  appName: 'Ember Twiddle',
  testCustomError: Ember.on('init', () => {
    let customErrorInstance = new CustomError();
    console.log(customErrorInstance);
  })
});

console.log(customErrorInstance):

CustomError {description: undefined, fileName: undefined, lineNumber: undefined, : " ". name: ""...}

+9

Source: https://habr.com/ru/post/1606394/


All Articles