trac-peer 0.1.29 → 0.1.30

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,7 +1,7 @@
1
1
  {
2
2
  "name": "trac-peer",
3
3
  "main": "src/index.js",
4
- "version": "0.1.29",
4
+ "version": "0.1.30",
5
5
  "type": "module",
6
6
  "dependencies": {
7
7
  "assert": "npm:bare-node-assert",
package/src/contract.js CHANGED
@@ -1,5 +1,3 @@
1
- import {AssertionError} from "bare-assert";
2
-
3
1
  class Contract {
4
2
 
5
3
  constructor(protocol, options = {}) {
@@ -10,6 +8,7 @@ class Contract {
10
8
  this.is_message = false;
11
9
  this.features = {};
12
10
  this.schemata = {};
11
+ this.funcs = {};
13
12
  this.message_handler = null;
14
13
  this.address = null;
15
14
  this.validator_address = null;
@@ -101,31 +100,33 @@ class Contract {
101
100
  try {
102
101
  _return = await this.message_handler();
103
102
  } catch(e) {
104
- if(!(e instanceof AssertionError)){
103
+ if(e.constructor.name !== 'AssertionError'){
105
104
  throw new RethrownError('Error in contract.', e);
106
105
  }
107
- _return = e.message;
106
+ _return = e;
108
107
  }
109
108
  }
110
- } else {
109
+ } else if(this[this.op.type] !== undefined) {
111
110
  try {
112
- if(this[this.op.type] !== undefined) {
113
- if(this.schemata[this.op.type] !== undefined){
114
- if(true === this.validateSchema(this.op.type, this.op)) {
115
- _return = await this[this.op.type]();
116
- } else {
117
- _return = false;
118
- }
119
- } else {
111
+ if(this.schemata[this.op.type] !== undefined){
112
+ if(true === this.validateSchema(this.op.type, this.op)) {
120
113
  _return = await this[this.op.type]();
114
+ } else {
115
+ _return = new UnknownContractOperationType('Invalid schema.');
121
116
  }
117
+ } else if(this.funcs[this.op.type] !== undefined) {
118
+ _return = await this[this.op.type]();
119
+ } else {
120
+ _return = new UnknownContractOperationType('Function not registered.');
122
121
  }
123
122
  } catch(e) {
124
- if(!(e instanceof AssertionError)){
123
+ if(e.constructor.name !== 'AssertionError'){
125
124
  throw new RethrownError('Error in contract.', e);
126
125
  }
127
- _return = e.message;
126
+ _return = e;
128
127
  }
128
+ } else {
129
+ _return = new UnknownContractOperationType('Unknown contract operation type.');
129
130
  }
130
131
 
131
132
  this.address = null;
@@ -145,6 +146,10 @@ class Contract {
145
146
  return result === true;
146
147
  }
147
148
 
149
+ register(type){
150
+ this.funcs[type] = true;
151
+ }
152
+
148
153
  addSchema(type, schema){
149
154
  this.schemata[type] = this.protocol.peer.check.validator.compile(schema);
150
155
  }
@@ -202,6 +207,12 @@ class Contract {
202
207
  }
203
208
  }
204
209
 
210
+ class UnknownContractOperationType extends Error {
211
+ constructor(message) {
212
+ super(message);
213
+ }
214
+ }
215
+
205
216
  class RethrownError extends Error {
206
217
  constructor(message, error){
207
218
  super(message)
package/src/index.js CHANGED
@@ -115,6 +115,7 @@ export class Peer extends ReadyResource {
115
115
  );
116
116
  let _err = null;
117
117
  if(null !== err) {
118
+ if(err.constructor.name === 'UnknownContractOperationType') continue;
118
119
  _err = ''+err.message;
119
120
  }
120
121
  let len = await batch.get('txl');
package/src/protocol.js CHANGED
@@ -164,7 +164,7 @@ class Protocol{
164
164
 
165
165
  getError(value){
166
166
  if (value === false || (value !== undefined && value.stack !== undefined && value.message !== undefined)) {
167
- return value === false ? new Error('Generic Error') : value;
167
+ return value === false ? new Error('Error') : value;
168
168
  }
169
169
  return null;
170
170
  }