DataDog / datadog-api-client-typescript

Typescript client for the Datadog API

Home Page:http://datadoghq.dev/datadog-api-client-typescript/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Uncatched API Exception in TeamsApi if StatusCode 403

Rbillon59 opened this issue · comments

Describe the bug

When trying to create teamMemberships with createTeamMembership even if surrounded by try catches like in

try {
        apiInstance.createTeamMembership(teamMembership);
      }
      catch (error) {
        throw new Error(`${error}`);
      }

The application is crashing with :

/workspace/node_modules/@datadog/datadog-api-client/dist/packages/datadog-api-client-v2/apis/TeamsApi.js:618
            throw new exception_1.ApiException(response.httpStatusCode, 'Unknown API Status Code!\nBody: "' + body + '"');
                  ^

ApiException [Error]: HTTP-Code: 403
Message: "Unknown API Status Code!\nBody: \"{\"errors\":[\"You do not have permission to do this.\"]}\""
    at TeamsApiResponseProcessor.<anonymous> (/workspace/node_modules/@datadog/datadog-api-client/dist/packages/datadog-api-client-v2/apis/TeamsApi.js:618:19)
    at Generator.next (<anonymous>)
    at fulfilled (/workspace/node_modules/@datadog/datadog-api-client/dist/packages/datadog-api-client-v2/apis/TeamsApi.js:5:58)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  code: 403,
  body: 'Unknown API Status Code!\n' +
    'Body: "{"errors":["You do not have permission to do this."]}"'
}

Node.js v18.16.1

To Reproduce
Steps to reproduce the behavior:

  1. Create a proper TeamsApiCreateTeamMembershipRequest like
{
    "body": {
        "data": {
            "attributes": {
                "role": "admin"
            },
            "relationships": {
                "user": {
                    "data": {
                        "id": "AN EXISTING USER ID",
                        "type": "users"
                    }
                }
            },
            "type": "team_memberships"
        }
    },
    "teamId": "AN EXISTING TEAM ID"
}
  1. Perform a createTeamMembership request with the said params

Expected behavior
I would expect the exception to be catched and not crashing the application.
Also the error message says Unknown API Status Code! while 403 is a known one

Environment and Versions (please complete the following information):
"@datadog/datadog-api-client": "^1.13.0",
Node.js v18.16.1

Additional context
The app key used misses some rights to perform that operation. It is known, I'm just talking about the API Exception management

Hi,

403 is missing for those endpoints, so that's why you're getting that error message. We'll fix it, but it won't make a big difference in the behavior, as you will still get a ApiException. There is no way to not raise it if the API returns 403.

I re-read your report, and the problem you're having is that the API returns a Promise, this is not a synchronous call. You either need to add the await keyword or do apiInstance.createTeamMembership(teamMembership).catch((error) => ...._).

Good catch, thank you. It's not crashing anymore now. I was not awaiting since I was considering doing the update asynchronously. I will close the issue.