loopbackio / loopback-next

LoopBack makes it easy to build modern API applications that require complex integrations.
https://loopback.io
Other
4.95k stars 1.07k forks source link

Catching errors from service calling external API #4182

Closed bobby91560 closed 3 years ago

bobby91560 commented 4 years ago

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

dhmlau commented 4 years ago

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

raymondfeng commented 4 years ago

If you are looking for a logging component, check out https://github.com/strongloop/loopback-next/pull/4117

bobby91560 commented 4 years ago

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

dhmlau commented 4 years ago

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

bobby91560 commented 4 years ago

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

bobby91560 commented 4 years ago

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

bobby91560 commented 4 years ago

Hi,

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

stale[bot] commented 3 years ago

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.