Skip to content

Commit d0a55b5

Browse files
authored
Fix-logging (#69)
* Better error when could not connect to runtime * Fix logging * Fixes
1 parent f1a9bb3 commit d0a55b5

5 files changed

Lines changed: 42 additions & 9 deletions

File tree

Source/sdk/ClientBuilder.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,10 @@ export class ClientBuilder {
6363
this._projectionsBuilder = new ProjectionsBuilder(this._projectionsAssociations);
6464
this._logger = createLogger({
6565
level: 'info',
66-
format: format.prettyPrint(),
67-
defaultMeta: { microserviceId: _microserviceId.toString() },
66+
format: format.simple(),
6867
transports: [
6968
new transports.Console({
70-
format: format.prettyPrint()
69+
format: format.simple()
7170
})
7271
]
7372
});

Source/services/ClientProcessor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export abstract class ClientProcessor<TIdentifier extends ConceptAs<Guid, string
5050
if (failure) {
5151
subscriber.error(new RegistrationFailed(this._kind, this._identifier.value, failures.toSDK(failure)!));
5252
} else {
53-
this._logger.debug(`${this._kind} ${this._identifier} registered with the Runtime, start handling requests.`);
53+
this._logger.info(`${this._kind} ${this._identifier} registered with the Runtime, start handling requests.`);
5454
}
5555
},
5656
error: (error: Error) => {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) Dolittle. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
import { Exception } from '@dolittle/rudiments';
5+
6+
/**
7+
* Exception that gets thrown when the a the connection to the Runtime could not be established.
8+
*/
9+
export class CouldNotConnectToRuntime extends Exception {
10+
11+
/**
12+
* Initializes a new instance of {@link CouldNotConnectToRuntime}.
13+
*/
14+
constructor(address: string) {
15+
super(`Could not connect to a Runtime on '${address}'. PLease make sure a Runtime is running, and that the private port (usually 50053) is accessible on the specified port.`);
16+
}
17+
}

Source/services/ReactiveGrpc.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Cancellation } from '@dolittle/sdk.resilience';
55
import * as grpc from '@grpc/grpc-js';
66
import { Observable, Subject } from 'rxjs';
77
import { concatMap } from 'rxjs/operators';
8+
import { CouldNotConnectToRuntime } from './CouldNotConnectToRuntime';
89
import { ClientStreamMethod, DuplexMethod, ServerStreamMethod, UnaryMethod } from './GrpcMethods';
910

1011

@@ -21,7 +22,7 @@ export function reactiveUnary<TArgument, TResponse>(client: grpc.Client, method:
2122
const metadata = new grpc.Metadata();
2223
const call = method.call(client, argument, metadata, {}, (error: grpc.ServiceError | null, message?: TResponse) => {
2324
if (error) {
24-
subject.error(error);
25+
subject.error(getErrorFromGrpc(error, client.getChannel().getTarget()));
2526
} else {
2627
subject.next(message);
2728
subject.complete();
@@ -44,14 +45,14 @@ export function reactiveClientStream<TRequest, TResponse>(client: grpc.Client, m
4445
const metadata = new grpc.Metadata();
4546
const stream = method.call(client, metadata, {}, (error: grpc.ServiceError | null, message?: TResponse) => {
4647
if (error) {
47-
subject.error(error);
48+
subject.error(getErrorFromGrpc(error, client.getChannel().getTarget()));
4849
} else {
4950
subject.next(message);
5051
subject.complete();
5152
}
5253
});
5354
handleCancellation(stream, cancellation);
54-
handleClientRequests(stream, requests, subject);
55+
handleClientRequests(stream, requests, subject, client.getChannel().getTarget());
5556
return subject;
5657
}
5758

@@ -84,7 +85,7 @@ export function reactiveDuplex<TRequest, TResponse>(client: grpc.Client, method:
8485
const metadata = new grpc.Metadata();
8586
const stream = method.call(client, metadata, {});
8687
handleCancellation(stream, cancellation);
87-
handleClientRequests(stream, requests, subject);
88+
handleClientRequests(stream, requests, subject, client.getChannel().getTarget());
8889
handleServerResponses(stream, subject);
8990
return subject;
9091
}
@@ -107,7 +108,7 @@ function handleCancellation(call: grpc.Call, cancellation: Cancellation) {
107108
* @param {Observable} requests The requests to write to the Runtime.
108109
* @param {Subject} subject The Subject which contains the responses from the Runtime.
109110
*/
110-
function handleClientRequests<TRequest, TResponse>(stream: grpc.ClientWritableStream<TRequest>, requests: Observable<TRequest>, subject: Subject<TResponse>) {
111+
function handleClientRequests<TRequest, TResponse>(stream: grpc.ClientWritableStream<TRequest>, requests: Observable<TRequest>, subject: Subject<TResponse>, address: string) {
111112
requests.pipe(concatMap((message: TRequest) => {
112113
const subject = new Subject<void>();
113114
stream.write(message, undefined, () => {
@@ -120,6 +121,9 @@ function handleClientRequests<TRequest, TResponse>(stream: grpc.ClientWritableSt
120121
},
121122
error: (error: any) => {
122123
stream.cancel();
124+
if (isGrpcError(error)) {
125+
error = getErrorFromGrpc(error, address);
126+
}
123127
subject.error(error);
124128
}
125129
});
@@ -141,3 +145,15 @@ function handleServerResponses<TResponse>(stream: grpc.ClientReadableStream<TRes
141145
subject.error(error);
142146
});
143147
}
148+
149+
function getErrorFromGrpc(error: grpc.ServiceError, address: string) {
150+
if (error.code === grpc.status.UNAVAILABLE) {
151+
return new CouldNotConnectToRuntime(address);
152+
} else {
153+
return error;
154+
}
155+
}
156+
157+
function isGrpcError(error: any): error is grpc.ServiceError {
158+
return error.code !== undefined;
159+
}

Source/services/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Dolittle. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4+
export { CouldNotConnectToRuntime } from './CouldNotConnectToRuntime';
45
export { DidNotReceiveConnectResponse } from './DidNotReceiveConnectResponse';
56
export { ClientStreamMethod, DuplexMethod, ServerStreamMethod, UnaryMethod } from './GrpcMethods';
67
export { IReverseCallClient, ReverseCallCallback } from './IReverseCallClient';

0 commit comments

Comments
 (0)