Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error catching and Typescript: Object is of type 'unknown' #320

Closed
soullivaneuh opened this issue Jul 7, 2022 · 5 comments
Closed

Error catching and Typescript: Object is of type 'unknown' #320

soullivaneuh opened this issue Jul 7, 2022 · 5 comments

Comments

@soullivaneuh
Copy link

Since I upgraded my project to Typescript 4.4+, any catch error is considered as unknown and its type has to be verified.

This article well explain the situation: https://bobbyhadz.com/blog/typescript-object-is-of-type-unknown

I have the case with this library. Considering the following code sample:

try {
  const mangoCard = await mango.Cards.get(card.cardId);
  // Stuff...
} catch (error) {
  if (error.Type === 'ressource_not_found') {
    // Stuff...
  }

  throw error;
}

Gives me the following Typescript error:

error TS2571: Object is of type 'unknown'.

208     if (error.Type === 'ressource_not_found') {
            ~~~~~

This should be something solvable by using instanceof with the right error instance type. For example with a Joi validation error catch:

import Joi, {
  ValidationError,
} from 'joi';

try {
  await schema.tailor(context.method).validateAsync(context.data, {
    abortEarly: false,
  });
} catch (e) {
  if (e instanceof ValidationError) {
    throw new BadRequest('invalid data', { errors: e.details });
  }

  throw e;
}

However, I did not find any typing representing the MangoPay error containing the Type property on this library.

Did I miss something or is it something that need to be updated on that project? 🤔

@fredericdelordm
Copy link
Contributor

Hello @soullivaneuh,

Thanks for the feedback and sharing all these details about your issue. The team is looking into it

@iulian03
Copy link
Contributor

iulian03 commented Aug 4, 2022

Hey @soullivaneuh,

I am going to add an ApiError type that should help.

type ApiError = {
            Message: string;
            Type: string;
            Id: string;
            Date: number;
            errors: [string, string];
        }

Also, you can define your custom error handler when instantiating the API:

const api = new mangopay({
    clientId: 'placeholder',
    clientApiKey: 'placeholder',
    errorHandler: function (errorMessage, errorBody: mangopay.models.ApiError) {
        console.log(errorMessage)
        console.log(errorBody)
    }
});

@fredericdelordm
Copy link
Contributor

Fix in release 1.30.1

@soullivaneuh
Copy link
Author

Currently have 1.31.1, but I don't find the ApiError export:

import {
  ApiError
} from 'mangopay2-nodejs-sdk';

How am I supposed to use it?

@RodolphePoon
Copy link

RodolphePoon commented Jun 20, 2023

@soullivaneuh

Currently have 1.31.1, but I don't find the ApiError export:

import {
  ApiError
} from 'mangopay2-nodejs-sdk';

How am I supposed to use it?

import * as Mangopay from 'mangopay2-nodejs-sdk';
let error : Mangopay.models.ApiError

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants