-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Catching errors from service calling external API #7177
-
Hello,
How can I catch an error and the request made when calling a external API using services and loopback-connector-rest ? My purpose is to log the response's error in a different way than Loopback.
Example of what I want to do :
try {
await this.someService.someMethod(someObject);
}
catch(e) {
logger.error(request);
}
Thanks in advance
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 9 comments 1 reply
-
@bobby91560, are you calling the service from the controller? If yes, then you can wrap that call within a try-catch block?
Beta Was this translation helpful? Give feedback.
All reactions
-
If you are looking for a logging component, check out #4117
Beta Was this translation helpful? Give feedback.
All reactions
-
@dhmlau That's what I already do.
With my team, we would like to catch all the errors when calling an external API, even when the API is shutdown. But it seems that the error is catch before, in the reject prodiver.
Also, I want to get the request made to the external API but I don't know how to get it. All I can get it's the request from the controller method.
Beta Was this translation helpful? Give feedback.
All reactions
-
@bobby91560 I also need to intercept the request made to the external api , how can i achieve this?
Beta Was this translation helpful? Give feedback.
All reactions
-
Also, I want to get the request made to the external API but I don't know how to get it. All I can get it's the request from the controller method.
It might make sense to use interceptor to get that information. @raymondfeng, WDYT?
FYI -
https://medium.com/loopback/learning-loopback-4-interceptors-part-1-global-interceptors-163a7c5701b9
https://medium.com/loopback/learning-loopback4-interceptors-part-2-class-and-method-level-interceptors-c80b401c16e4
Beta Was this translation helpful? Give feedback.
All reactions
-
Thank for your response. I don't know why, but now I can catch the error. Maybe a wrong manipulation.
I will give it a try for the interceptor, I'll keep you in touch if i make some progress or not.
Ps: Thanks for the links
Beta Was this translation helpful? Give feedback.
All reactions
-
@dhmlau I tried to use interceptor to get the request but it doesn't seem to work when using services and loopback-connector-rest.
I have this service :
import {getService} from '@loopback/service-proxy';
import {inject, Provider, intercept} from '@loopback/core';
import {UlimDataSource} from '../datasources/ulim.datasource';
export interface UserReference {
id: string;
login: string;
userId: string;
accountId: string;
createdAt: string;
createdBy: string;
updatedAt: string;
updatedBy: string;
}
export interface UlimService {
createUserReference(body: object): Promise<UserReference>;
updateAccountId(id: string, body: object): Promise<void>;
getUserReference(id: string): Promise<UserReference>;
delUserReference(id: string): Promise<void>;
}
@intercept(UamServiceLoggerInterceptor)
export class UlimServiceProvider implements Provider<UlimService> {
constructor(
// ulim must match the name property in the datasource json file
@inject('datasources.ulim')
protected dataSource: UlimDataSource = new UlimDataSource(),
) {}
value(): Promise<UlimService> {
return getService(this.dataSource);
}
}
And this Interceptor :
import {
Interceptor,
} from '@loopback/context';
import { RestBindings } from '@loopback/rest';
export const UamServiceLoggerInterceptor: Interceptor = async (invocationCtx, next) => {
const req = await invocationCtx.get(RestBindings.Http.REQUEST);
console.log(req);
try {
const result = await next();
const res = await invocationCtx.get(RestBindings.Http.RESPONSE);
console.log(res);
return result;
} catch (err) {
console.log(err);
throw err;
}
};
What did I miss ?
Beta Was this translation helpful? Give feedback.
All reactions
-
Hi,
I repeat the same question as above. Did you have some clues to achieve what I want ?
Beta Was this translation helpful? Give feedback.
All reactions
-
This issue has been marked stale because it has not seen activity within six months. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS
file at the top-level of this repository. This issue will be closed within 30 days of being stale.
Beta Was this translation helpful? Give feedback.
All reactions
-
happen to me as well, like error happend in service or controller, cannot catch this error by interceptor or middle ware
Beta Was this translation helpful? Give feedback.