Skip to content

Fix empty attachments 500 error in NPM package PUT request

What does this MR do and why?

When the API path PUT /api/:version/projects/:id/packages/npm/:package_name is called and the body of the request contains an attachment with a data field that is an empty string the API returns a 500 error with the message undefined method count' for nil:NilClass`.

Solution

Catch the error and raise 400 error instead of 500 with a message explaining the reason for the error to the user: {"message":"Attachment data is empty."}

Screenshots or screen recordings

Screenshots are required for UI changes, and strongly recommended for all other merge requests.

How to set up and validate locally

  1. Have a project where the packages feature is enabled
  2. Create a payload.json file with the following payload
payload
{
    "_attachments": {
        "foobar-1.0.0.tgz": {
            "data": ""
        }
    },
    "_id": "foobar@1.0.0",
    "name": "foobar",
    "dist-tags": {
        "latest": "1.0.2"
    },
    "versions": {
        "1.0.0": {
            "name": "foobar",
            "version": "1.0.0",
            "dist": {
                "shasum": "675ea51efc43db9e74c8c6b354adc2f5db1d0221"
            },
            "tarball": "https://example.com/foobar.tgz",
            "_id": "foobar@1.0.0"
        }
    }
}
3. Run this curl command (with a valid token and project ID):
curl --request PUT --header "Content-Type: application/json" --data @./payload.json --header "Authorization: Bearer your-token-here" http://gdk.test:3000/api/v4/projects/:project_id_here/packages/npm/foobar
  1. You should receive a 400 error with {"message":"Attachment data is empty."}. On master branch you would get 500 error.

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Related to #403162 (closed)

Edited by Moaz Khalifa

Merge request reports