try-block

Try in Playground
python-templatesUnknownInformational

0

No tags

No CWE or CVE

Catch generic exception

Ast Rule: try block


try-block

How to write a rule
function visit(node, filename, code) {
  // If filename starts or ends with test_ or _test, do not do anything
  if(filename.includes("_test.py") || filename.startsWith("test_")) {
    return;
  }

  // Get all the exception clauses with just a name equal to "Exception" 
  const genericExceptions = node.exceptClauses.flatMap(e => e.exceptions.filter(e => e.str === "Exception"));

  // For each generic exception
  genericExceptions.forEach(exception => {
    
    // report an error there the exception is
    const error = buildError(exception.start.line, exception.start.col, exception.end.line, exception.end.col, "generic exception", "WARNING", "BEST_PRACTICES");
    addError(error);
  });
}

try-exception.py

Expected test result: has error

try:
  pass
except Exception:
  pass
Add comment

Log in to add a comment


    Be the first one to leave a comment!

Codiga Logo
Codiga Hub
  • Rulesets
  • Explore
  • Cookbooks
  • Playground
soc-2 icon

We are SOC-2 Compliance Certified

G2 high performer medal

Codiga – All rights reserved 2022.