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

GitbeakerRequestError.cause.description has wrong type #3646

Open
2 tasks done
nhollander-alert opened this issue Oct 21, 2024 · 0 comments · May be fixed by #3654
Open
2 tasks done

GitbeakerRequestError.cause.description has wrong type #3646

nhollander-alert opened this issue Oct 21, 2024 · 0 comments · May be fixed by #3654
Labels
type:bug Changes fix a minor bug

Comments

@nhollander-alert
Copy link
Contributor

nhollander-alert commented Oct 21, 2024

Description

  • Node.js version: 22.8.0
  • Gitbeaker version: 40.6.0
  • Gitbeaker release (cli, rest, core, requester-utils): rest
  • OS & version: Alpine 3.20.2

the GitbeakerRequestError object has a property cause.description which is defined in GitbeakerError.ts:4 as a string, however at runtime this value contains an object.

Steps to reproduce

Run the following code snippet (replacing the token and project ID values with any project you have access to).

import { GitbeakerRequestError } from "@gitbeaker/requester-utils";
import { Gitlab } from "@gitbeaker/rest";

const gl = new Gitlab({
    token: "glpat-..."
});

try {
    await gl.Pipelines.create(12345, "invalid_ref");
} catch(e) {
    if(e instanceof GitbeakerRequestError) {
        console.log("Description type:", typeof e.cause?.description);
        console.log("Description value:", e.cause?.description);
    }
}

Expected behaviour

The program should print out

Description type: string
Description value: Reference not found

Actual behaviour

The program will output

Description type: object
Description value: { base: [ 'Reference not found' ] }

Possible fixes

I believe that the issue is caused by the handling of the description field in Requester.ts. The issue could probably be fixed by checking the type of the message field and changing the resolution behavior based on its value.

Gitlab's error message formatting is somewhat inconsistent and not always included in the API documentation so it might be the case that there isn't a guaranteed way to get the core error message, but it might be enough to simply check the type of the message field and if it is not a string, return a JSON stringified version of the response object.

@@ -57,7 +57,9 @@
     if (contentType?.includes('application/json')) {
       const output = JSON.parse(content);
   
-      description = output.message;
+      description = typeof output.message === "string"
+        ? output.message
+        : JSON.stringify(output);
     } else {
       description = content;
     }

Checklist

  • I have checked that this is not a duplicate issue.
  • I have read the documentation.
@jdalrymple jdalrymple added the type:bug Changes fix a minor bug label Oct 30, 2024
@jdalrymple jdalrymple linked a pull request Nov 2, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:bug Changes fix a minor bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants