@toa.io/core 1.0.0-alpha.56 → 1.0.0-alpha.59

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@toa.io/core",
3
- "version": "1.0.0-alpha.56",
3
+ "version": "1.0.0-alpha.59",
4
4
  "description": "Toa Core",
5
5
  "author": "temich <tema.gurtovoy@gmail.com>",
6
6
  "homepage": "https://github.com/toa-io/toa#readme",
@@ -21,13 +21,13 @@
21
21
  },
22
22
  "dependencies": {
23
23
  "@rsql/parser": "1.2.4",
24
- "@toa.io/console": "1.0.0-alpha.56",
25
- "@toa.io/generic": "1.0.0-alpha.56",
26
- "@toa.io/yaml": "1.0.0-alpha.56",
24
+ "@toa.io/console": "1.0.0-alpha.59",
25
+ "@toa.io/generic": "1.0.0-alpha.59",
26
+ "@toa.io/yaml": "1.0.0-alpha.59",
27
27
  "error-value": "0.3.0"
28
28
  },
29
29
  "devDependencies": {
30
30
  "clone-deep": "4.0.1"
31
31
  },
32
- "gitHead": "15ee832b26d9893fdbcf6bde1b081b5fddf8d6da"
32
+ "gitHead": "157e7fa2e26f4612bbb7848c420f4c21746e5bfd"
33
33
  }
@@ -1,5 +1,6 @@
1
1
  'use strict'
2
2
 
3
+ const { Readable } = require('node:stream')
3
4
  const { SystemException } = require('../exceptions')
4
5
 
5
6
  class Contract {
@@ -13,7 +14,7 @@ class Contract {
13
14
  const error = this.schema.fit(value)
14
15
 
15
16
  if (error !== null)
16
- throw new this.constructor.Exception(error)
17
+ throw new this.constructor.Exception(error, value)
17
18
  }
18
19
 
19
20
  static Exception = SystemException
@@ -21,10 +21,10 @@ class Changeset {
21
21
  }
22
22
 
23
23
  set (value) {
24
- const error = this.#schema.fitOptional(value)
24
+ const error = this.#schema.match(value)
25
25
 
26
26
  if (error !== null)
27
- throw new EntityContractException(error)
27
+ throw new EntityContractException(error, value)
28
28
 
29
29
  delete value._version
30
30
  value._updated = Date.now()
@@ -29,7 +29,7 @@ class Entity {
29
29
  const error = optional ? this.#schema.fitOptional(value) : this.#schema.fit(value)
30
30
 
31
31
  if (error !== null)
32
- throw new EntityContractException(error)
32
+ throw new EntityContractException(error, value)
33
33
 
34
34
  this.#set(value)
35
35
  }
package/src/exceptions.js CHANGED
@@ -31,9 +31,12 @@ class Exception {
31
31
  code
32
32
  message
33
33
 
34
- constructor (code, message) {
34
+ constructor (code, message, cause) {
35
35
  this.code = code
36
36
  this.message = message
37
+
38
+ if (cause !== undefined)
39
+ this.cause = cause
37
40
  }
38
41
  }
39
42
 
@@ -48,8 +51,8 @@ class SystemException extends Exception {
48
51
  }
49
52
 
50
53
  class ContractException extends Exception {
51
- constructor (code, error) {
52
- super(code || codes.Contract, typeof error === 'string' ? error : error?.message)
54
+ constructor (code, error, cause) {
55
+ super(code || codes.Contract, typeof error === 'string' ? error : error?.message, cause)
53
56
 
54
57
  if (typeof error === 'object' && error !== null)
55
58
  for (const k of ['keyword', 'property', 'schema', 'path', 'params'])
@@ -59,15 +62,15 @@ class ContractException extends Exception {
59
62
  }
60
63
 
61
64
  class RequestContractException extends ContractException {
62
- constructor (error) { super(codes.RequestContract, error) }
65
+ constructor (error, cause) { super(codes.RequestContract, error, cause) }
63
66
  }
64
67
 
65
68
  class ResponseContractException extends ContractException {
66
- constructor (error) { super(codes.ResponseContract, error) }
69
+ constructor (error, cause) { super(codes.ResponseContract, error, cause) }
67
70
  }
68
71
 
69
72
  class EntityContractException extends ContractException {
70
- constructor (error) { super(codes.EntityContract, error) }
73
+ constructor (error, cause) { super(codes.EntityContract, error, cause) }
71
74
  }
72
75
 
73
76
  // #region exports
@@ -82,8 +85,12 @@ for (const [name, code] of Object.entries(codes)) {
82
85
 
83
86
  if (exports[classname] === undefined) {
84
87
  exports[classname] = class extends Exception {
85
- constructor (message) {
86
- super(code, message ?? classname)
88
+ constructor (message, cause) {
89
+ message = message
90
+ ? `${classname}: ${message}`
91
+ : classname
92
+
93
+ super(code, message ?? classname, cause)
87
94
  }
88
95
  }
89
96
  }
package/src/operation.js CHANGED
@@ -73,7 +73,7 @@ class Operation extends Connector {
73
73
  const reply = await this.#cascade.run(request.input, state)
74
74
 
75
75
  // validate reply only on local environments
76
- if (process.env.TOA_ENV === 'local')
76
+ if (process.env.TOA_ENV === 'local' && !(reply instanceof Readable))
77
77
  this.#contracts.reply.fit(reply)
78
78
 
79
79
  store.reply = reply