wrapped.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. 'use strict';
  2. var test = require('tape');
  3. var net = require('net');
  4. var WrappedError = require('../wrapped.js');
  5. test('can create a wrapped error', function t(assert) {
  6. var ServerListenError = WrappedError({
  7. message: 'server: {causeMessage}',
  8. type: 'server.listen-failed',
  9. requestedPort: null,
  10. host: null
  11. });
  12. var err = new Error('listen EADDRINUSE');
  13. err.code = 'EADDRINUSE';
  14. var err2 = ServerListenError(err, {
  15. requestedPort: 3426,
  16. host: 'localhost'
  17. });
  18. assert.equal(ServerListenError.type, 'server.listen-failed');
  19. assert.equal(err2.message, 'server: listen EADDRINUSE');
  20. assert.equal(err2.requestedPort, 3426);
  21. assert.equal(err2.host, 'localhost');
  22. assert.equal(err2.code, 'EADDRINUSE');
  23. assert.equal(err2.cause, err);
  24. assert.equal(err2.toString(),
  25. 'ServerListenFailedError: server: listen EADDRINUSE');
  26. assert.equal(JSON.stringify(err2), JSON.stringify({
  27. type: 'server.listen-failed',
  28. message: 'server: listen EADDRINUSE',
  29. requestedPort: 3426,
  30. host: 'localhost',
  31. name: 'ServerListenFailedError',
  32. causeMessage: 'listen EADDRINUSE',
  33. origMessage: 'listen EADDRINUSE',
  34. code: 'EADDRINUSE',
  35. fullType: 'server.listen-failed~!~error.wrapped-unknown'
  36. }));
  37. assert.end();
  38. });
  39. test('can create wrapped error with syscall', function t(assert) {
  40. var SysCallError = WrappedError({
  41. 'message': 'tchannel socket error ({code} from ' +
  42. '{syscall}): {origMessage}',
  43. type: 'syscall.error'
  44. });
  45. var err = new Error('listen EADDRINUSE');
  46. err.code = 'EADDRINUSE';
  47. err.syscall = 'listen';
  48. var err2 = SysCallError(err);
  49. assert.equal(err2.message, 'tchannel socket error ' +
  50. '(EADDRINUSE from listen): listen EADDRINUSE');
  51. assert.equal(err2.syscall, 'listen');
  52. assert.equal(err2.code, 'EADDRINUSE');
  53. assert.equal(err2.type, 'syscall.error');
  54. assert.end();
  55. });
  56. test('wrapping twice', function t(assert) {
  57. var ReadError = WrappedError({
  58. type: 'my.read-error',
  59. message: 'read: {causeMessage}'
  60. });
  61. var DatabaseError = WrappedError({
  62. type: 'my.database-error',
  63. message: 'db: {causeMessage}'
  64. });
  65. var BusinessError = WrappedError({
  66. type: 'my.business-error',
  67. message: 'business: {causeMessage}'
  68. });
  69. var err = BusinessError(
  70. DatabaseError(
  71. ReadError(
  72. new Error('oops')
  73. )
  74. )
  75. );
  76. assert.ok(err);
  77. assert.equal(err.message, 'business: db: read: oops');
  78. assert.equal(err.type, 'my.business-error');
  79. assert.equal(err.fullType, 'my.business-error~!~' +
  80. 'my.database-error~!~' +
  81. 'my.read-error~!~' +
  82. 'error.wrapped-unknown');
  83. assert.end();
  84. });
  85. test('handles bad recursive strings', function t(assert) {
  86. var ReadError = WrappedError({
  87. type: 'wat.wat',
  88. message: 'read: {causeMessage}'
  89. });
  90. var err2 = ReadError(new Error('hi {causeMessage}'));
  91. assert.ok(err2);
  92. assert.equal(err2.message,
  93. 'read: hi $INVALID_CAUSE_MESSAGE_LITERAL');
  94. assert.end();
  95. });
  96. test('can wrap real IO errors', function t(assert) {
  97. var ServerListenError = WrappedError({
  98. message: 'server: {causeMessage}',
  99. type: 'server.listen-failed',
  100. requestedPort: null,
  101. host: null
  102. });
  103. var otherServer = net.createServer();
  104. otherServer.once('listening', onPortAllocated);
  105. otherServer.listen(0);
  106. function onPortAllocated() {
  107. var port = otherServer.address().port;
  108. var server = net.createServer();
  109. server.on('error', onError);
  110. server.listen(port);
  111. function onError(cause) {
  112. var err = ServerListenError(cause, {
  113. host: 'localhost',
  114. requestedPort: port
  115. });
  116. otherServer.close();
  117. assertOnError(err, cause, port);
  118. }
  119. }
  120. function assertOnError(err, cause, port) {
  121. assert.equal(err.message, 'server: listen EADDRINUSE');
  122. assert.equal(err.requestedPort, port);
  123. assert.equal(err.host, 'localhost');
  124. assert.equal(err.code, 'EADDRINUSE');
  125. assert.equal(err.cause, cause);
  126. assert.equal(err.toString(),
  127. 'ServerListenFailedError: server: listen EADDRINUSE');
  128. assert.equal(JSON.stringify(err), JSON.stringify({
  129. type: 'server.listen-failed',
  130. message: 'server: listen EADDRINUSE',
  131. requestedPort: port,
  132. host: 'localhost',
  133. name: 'ServerListenFailedError',
  134. causeMessage: 'listen EADDRINUSE',
  135. origMessage: 'listen EADDRINUSE',
  136. code: 'EADDRINUSE',
  137. errno: 'EADDRINUSE',
  138. syscall: 'listen',
  139. fullType: 'server.listen-failed~!~' +
  140. 'error.wrapped-io.listen.EADDRINUSE'
  141. }));
  142. assert.end();
  143. }
  144. });