Skip to content

Commit 67a1f91

Browse files
authored
Fix issue with avm logs not being displayed (#54)
1 parent 2e016e9 commit 67a1f91

File tree

9 files changed

+77
-33
lines changed

9 files changed

+77
-33
lines changed

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"author": "Fluence Labs",
2020
"license": "Apache-2.0",
2121
"dependencies": {
22-
"@fluencelabs/avm": "0.9.12",
22+
"@fluencelabs/avm": "0.10.2",
2323
"async": "3.2.0",
2424
"base64-js": "1.3.1",
2525
"bs58": "4.0.1",

src/__test__/integration/builtins.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ describe('Builtins usage suite', () => {
8585
let promise = createService(client, 'test_broken_blueprint');
8686

8787
await expect(promise).rejects.toMatchObject({
88-
error: expect.stringContaining("Blueprint 'test_broken_blueprint' wasn't found"),
88+
msg: expect.stringContaining("Blueprint 'test_broken_blueprint' wasn't found"),
8989
instruction: expect.stringContaining('blueprint_id'),
9090
});
9191
});

src/__test__/integration/client.spec.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { checkConnection, createClient, FluenceClient } from '../../FluenceClien
22
import Multiaddr from 'multiaddr';
33
import { nodes } from '../connection';
44
import { RequestFlowBuilder } from '../../internal/RequestFlowBuilder';
5-
import { error } from 'loglevel';
65

76
let client: FluenceClient;
87

@@ -203,7 +202,7 @@ describe('Typescript usage suite', () => {
203202

204203
// assert
205204
await expect(promise).rejects.toMatchObject({
206-
error: expect.stringContaining("Service with id 'incorrect' not found"),
205+
msg: expect.stringContaining("Service with id 'incorrect' not found"),
207206
instruction: expect.stringContaining('incorrect'),
208207
});
209208
});
@@ -241,7 +240,7 @@ describe('Typescript usage suite', () => {
241240

242241
// assert
243242
await expect(res).rejects.toMatchObject({
244-
error: "Local service error: ret_code is 1024, error message is '\"The handler did not set any result. Make sure you are calling the right peer and the handler has been registered. Original request data was: serviceId='peer' fnName='identify' args=''\"'",
243+
msg: "Local service error: ret_code is 1024, error message is '\"The handler did not set any result. Make sure you are calling the right peer and the handler has been registered. Original request data was: serviceId='peer' fnName='identify' args=''\"'",
245244
instruction: 'call %init_peer_id% ("peer" "identify") [] res',
246245
});
247246
});

src/__test__/integration/legacy.api.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ describe('Legacy api suite', () => {
4646
});
4747

4848
await expect(promise).rejects.toMatchObject({
49-
error: expect.stringContaining("Service with id 'incorrect' not found"),
49+
msg: expect.stringContaining("Service with id 'incorrect' not found"),
5050
instruction: expect.stringContaining('incorrect'),
5151
});
5252
});
@@ -78,7 +78,7 @@ describe('Legacy api suite', () => {
7878
const promise = sendParticleAsFetch<[string]>(client, new Particle(script), 'fn', 'service');
7979

8080
await expect(promise).rejects.toMatchObject({
81-
error: expect.stringContaining("Service with id 'incorrect' not found"),
81+
msg: expect.stringContaining("Service with id 'incorrect' not found"),
8282
instruction: expect.stringContaining('incorrect'),
8383
});
8484
});

src/internal/ClientImpl.ts

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,60 @@ import { CallServiceHandler } from './CallServiceHandler';
2525
import { loadRelayFn, loadVariablesService } from './RequestFlowBuilder';
2626
import { logParticle, Particle } from './particle';
2727
import log from 'loglevel';
28-
import { AirInterpreter, ParticleHandler, SecurityTetraplet, CallServiceResult } from '@fluencelabs/avm';
28+
import {
29+
AirInterpreter,
30+
ParticleHandler,
31+
SecurityTetraplet,
32+
CallServiceResult,
33+
LogLevel as AvmLogLevel,
34+
} from '@fluencelabs/avm';
2935
import makeDefaultClientHandler from './defaultClientHandler';
3036

37+
const createClient = (handler, peerId): Promise<AirInterpreter> => {
38+
let logLevel: AvmLogLevel = 'off';
39+
switch (log.getLevel()) {
40+
case 0: // 'TRACE'
41+
logLevel = 'trace';
42+
break;
43+
case 1: // 'DEBUG'
44+
logLevel = 'debug';
45+
break;
46+
case 2: // 'INFO'
47+
logLevel = 'info';
48+
break;
49+
case 3: // 'WARN'
50+
logLevel = 'warn';
51+
break;
52+
case 4: // 'ERROR'
53+
logLevel = 'error';
54+
break;
55+
case 5: // 'SILENT'
56+
logLevel = 'off';
57+
break;
58+
}
59+
const logFn = (level: AvmLogLevel, msg: string) => {
60+
switch (level) {
61+
case 'error':
62+
log.error(msg);
63+
break;
64+
65+
case 'warn':
66+
log.warn(msg);
67+
break;
68+
69+
case 'info':
70+
log.info(msg);
71+
break;
72+
73+
case 'debug':
74+
case 'trace':
75+
log.log(msg);
76+
break;
77+
}
78+
};
79+
return AirInterpreter.create(handler, peerId, logLevel, logFn);
80+
};
81+
3182
export class ClientImpl implements FluenceClient {
3283
readonly selfPeerIdFull: PeerId;
3384

@@ -68,12 +119,7 @@ export class ClientImpl implements FluenceClient {
68119
}
69120

70121
async initAirInterpreter(): Promise<void> {
71-
this.interpreter = await AirInterpreter.create(
72-
this.interpreterCallback.bind(this),
73-
this.selfPeerId,
74-
'trace',
75-
log.log,
76-
);
122+
this.interpreter = await createClient(this.interpreterCallback.bind(this), this.selfPeerId);
77123
}
78124

79125
async connect(multiaddr: string | Multiaddr, options?: FluenceConnectionOptions): Promise<void> {

src/internal/FluenceConnection.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import Peer from 'libp2p';
2020
import { decode, encode } from 'it-length-prefixed';
2121
import pipe from 'it-pipe';
2222
import * as log from 'loglevel';
23-
import { parseParticle, Particle, toPayload } from './particle';
23+
import { logParticle, parseParticle, Particle, toPayload } from './particle';
2424
import { NOISE } from 'libp2p-noise';
2525
import PeerId from 'peer-id';
2626
import Multiaddr from 'multiaddr';
@@ -114,7 +114,7 @@ export class FluenceConnection {
114114
if (this.status === Status.Initializing) {
115115
await this.node.start();
116116

117-
log.trace(`dialing to the node with client's address: ` + this.node.peerId.toB58String());
117+
log.debug(`dialing to the node with client's address: ` + this.node.peerId.toB58String());
118118

119119
try {
120120
await this.node.dial(this.address);
@@ -127,15 +127,13 @@ export class FluenceConnection {
127127
}
128128
}
129129

130-
let _this = this;
131-
132130
this.node.handle([PROTOCOL_NAME], async ({ connection, stream }) => {
133-
pipe(stream.source, decode(), async function (source: AsyncIterable<string>) {
131+
pipe(stream.source, decode(), async (source: AsyncIterable<string>) => {
134132
for await (const msg of source) {
135133
try {
136-
let particle = parseParticle(msg);
137-
log.trace('Particle is received:', JSON.stringify(particle, undefined, 2));
138-
_this.handleParticle(particle);
134+
const particle = parseParticle(msg);
135+
logParticle(log.debug, 'Particle is received:', particle);
136+
this.handleParticle(particle);
139137
} catch (e) {
140138
log.error('error on handling a new incoming message: ' + e);
141139
}
@@ -165,7 +163,7 @@ export class FluenceConnection {
165163

166164
let action = toPayload(particle);
167165
let particleStr = JSON.stringify(action);
168-
log.debug('send particle: \n' + JSON.stringify(action, undefined, 2));
166+
logParticle(log.debug, 'send particle: \n', particle);
169167

170168
// create outgoing substream
171169
const conn = (await this.node.dialProtocol(this.address, PROTOCOL_NAME)) as {

src/internal/RequestFlowBuilder.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,15 +195,14 @@ export class RequestFlowBuilder {
195195

196196
this.configHandler((h, request) => {
197197
h.onEvent(xorHandleService, xorHandleFn, (args) => {
198-
let msg;
199-
try {
200-
msg = JSON.parse(args[0]);
201-
} catch (e) {
202-
msg = e;
198+
if (args[0] === undefined) {
199+
log.error(
200+
'Request flow error handler recieved unexpected argument, value of %last_error% is undefined',
201+
);
203202
}
204203

205204
try {
206-
request.raiseError(msg);
205+
request.raiseError(args[0]);
207206
} catch (e) {
208207
log.error('Error handling script executed with error', e);
209208
}

src/internal/particle.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ export interface Particle {
3232
}
3333

3434
export const logParticle = (fn: Function, message: string, particle: Particle) => {
35-
fn(message, particle);
35+
const toLog = { ...particle };
36+
delete toLog.data;
37+
fn(message, toLog);
3638
};
3739

3840
/**

0 commit comments

Comments
 (0)