errors.test.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. 'use strict';
  2. const expect = require('chai').expect;
  3. const errors = require('@sequelize/core/_non-semver-use-at-your-own-risk_/errors/index.js');
  4. const { AggregateError } = errors;
  5. describe('errors', () => {
  6. it('should maintain stack trace with message', () => {
  7. const errorsWithMessage = [
  8. 'BaseError',
  9. 'ValidationError',
  10. 'InstanceError',
  11. 'EmptyResultError',
  12. 'EagerLoadingError',
  13. 'AssociationError',
  14. 'QueryError',
  15. ];
  16. for (const errorName of errorsWithMessage) {
  17. function throwError() {
  18. throw new errors[errorName]('this is a message');
  19. }
  20. let err;
  21. try {
  22. throwError();
  23. } catch (error) {
  24. err = error;
  25. }
  26. expect(err).to.exist;
  27. const stackParts = err.stack.split('\n');
  28. const fullErrorName = `Sequelize${errorName}`;
  29. expect(stackParts[0]).to.equal(`${fullErrorName}: this is a message`);
  30. expect(stackParts[1]).to.match(/^ {4}at throwError \(.*errors.test.js:\d+:\d+\)$/);
  31. }
  32. });
  33. describe('AggregateError', () => {
  34. it('get .message works', () => {
  35. expect(
  36. String(
  37. new AggregateError([
  38. new Error('foo'),
  39. new Error('bar\nbaz'),
  40. new AggregateError([new Error('this\nis\na\ntest'), new Error('qux')]),
  41. ]),
  42. ),
  43. ).to.equal(
  44. `AggregateError of:
  45. Error: foo
  46. Error: bar
  47. baz
  48. AggregateError of:
  49. Error: this
  50. is
  51. a
  52. test
  53. Error: qux
  54. `,
  55. );
  56. });
  57. });
  58. });