Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Catching errors from service calling external API #7177

Unanswered
bobby91560 asked this question in Q&A
Discussion options

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

You must be logged in to vote

Replies: 9 comments 1 reply

Comment options

@bobby91560, are you calling the service from the controller? If yes, then you can wrap that call within a try-catch block?

You must be logged in to vote
0 replies
Comment options

If you are looking for a logging component, check out #4117

You must be logged in to vote
0 replies
Comment options

@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.

You must be logged in to vote
1 reply
Comment options

@bobby91560 I also need to intercept the request made to the external api , how can i achieve this?

Comment options

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

You must be logged in to vote
0 replies
Comment options

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

You must be logged in to vote
0 replies
Comment options

@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 ?

You must be logged in to vote
0 replies
Comment options

Hi,

I repeat the same question as above. Did you have some clues to achieve what I want ?

You must be logged in to vote
0 replies
Comment options

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.

You must be logged in to vote
0 replies
Comment options

happen to me as well, like error happend in service or controller, cannot catch this error by interceptor or middle ware

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
Converted from issue

This discussion was converted from issue #4182 on March 11, 2021 13:42.

AltStyle によって変換されたページ (->オリジナル) /