Skip to content

Catching errors from service calling external API #4182

Closed

Description

@bobby91560

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

Activity

  1. dhmlau commented on Nov 22, 2019

    @dhmlau
    Member

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

  2. self-assigned this
    on Nov 22, 2019
  3. raymondfeng commented on Nov 22, 2019

    @raymondfeng
    Contributor

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

  4. bobby91560 commented on Nov 22, 2019

    @bobby91560
    Author

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

  5. dhmlau commented on Nov 22, 2019

    @dhmlau
    Member

    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

  6. bobby91560 commented on Nov 22, 2019

    @bobby91560
    Author

    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

  7. bobby91560 commented on Nov 25, 2019

    @bobby91560
    Author

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

  8. bobby91560 commented on Dec 2, 2019

    @bobby91560
    Author

    Hi,

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

  9. stale commented on Dec 25, 2020

    @stale

    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.

  10. locked and limited conversation to collaborators on Mar 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions