Skip to content
This repository has been archived by the owner on Aug 7, 2023. It is now read-only.

CodeClimate JSON Formatter Update Causing Getting Error [Linter] Error running Code Climate TypeError: Cannot convert undefined or null to object #76

Closed
a-ali opened this issue Dec 5, 2017 · 3 comments · Fixed by #83

Comments

@a-ali
Copy link

a-ali commented Dec 5, 2017

I'm using CodeClimate version 0.70.3

When running the linter on a ruby file, I got this error on the console
[Linter] Error running Code Climate TypeError: Cannot convert undefined or null to object

After some investigation, the root cause was this line in index.js file.
It's trying to access issue.location, but the issue object has no location key.

That's the output of running codeclimate -f json /path/to/file.rb

[
  {
    "name":"ruby.parse.succeeded",
    "type":"measurement",
    "value":1,
    "engine_name":"structure"
  },
  {
    "type":"Issue",
    "check_name":"sql_injection",
    "description":"Possible SQL injection",
    "fingerprint":"...",
    "categories":[
      "Security"
    ],
    "severity":"minor",
    "remediation_points":300000,
    "location":{
      "path":"/path/to/file.rb",
      "lines":{
        "begin":23,
        "end":23
      }
    },
    "content":{
      "body":"..."
    },
    "engine_name":"brakeman"
  },
  {
    "type":"Issue",
    "check_name":"Rubocop/Metrics/CyclomaticComplexity",
    "description":"Cyclomatic complexity for load_resources is too high. [8/6]",
    "categories":[
      "Complexity"
    ],
    "remediation_points":1140000,
    "location":{
      "path":"/path/to/file.rb",
      "positions":{
        "begin":{
          "column":3,
          "line":107
        },
        "end":{
          "column":6,
          "line":114
        }
      }
    },
    "content":{
      "body":"..."
    },
    "fingerprint":"...",
    "engine_name":"rubocop",
    "severity":"minor"
  }
]

You can notice that the first element in the output is of type measurments not Issue, this has been recently introduced in codeclimate json formatter.

So as a quick solution, I just added this condition inside the forEach block

if (issue.type !== 'Issue') {
  return;
}

We may also filter the messages before calling forEach.

Do you have a specific guide to create a pull request with this fix?

@maxjacobson
Copy link
Contributor

Great catch! I think your proposal looks correct and I’d be happy to merge a PR that makes that change. Ideally, you would include a test that exercises this logic, but if that proves difficult please open the PR anyway

@a-ali
Copy link
Author

a-ali commented Dec 7, 2017

Actually, the test was failing with this message

The codeclimate provider for Linter
  it works with a valid .codeclimate.yml file
    Expected promise to be resolved, but it was rejected with: Cannot convert undefined or null to object {  }

But it passes now.
Since the test is checking for the messages returned from the linter, not from codeclimate, I couldn't write a scenario for it.

@cgalvarez
Copy link
Contributor

The error reported by the test has to do with the promises timeout. You don't need to write an scenario for it. I describe how to fix this at #78

inakiabt added a commit to inakiabt/linter-codeclimate that referenced this issue Mar 6, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
3 participants