Skip to content

Swallowed errors: except: pass, an empty catch, catch (Exception e) { return null; }, Go's if err != nil { return nil } ​

Errors and checks hidden: one of the ways AI coding agents bloat software, from lessloc's pattern catalog. lessloc catches it as swallowed-error.

What makes swallowed errors a bad practice? ​

The standards that call it a bad practice: CWE-390 Detection of Error Condition Without Action; CWE-1069 Empty Exception Block; CWE-396.

What does lessloc do about swallowed errors? ​

lessloc catches it as swallowed-error.

  • swallowed-error (tier 1): An error caught and dropped, such as except Exception: pass or an empty catch. The agent hears about it right after the write.

Examples ​

Caught as swallowed-error:

js
export function read(f) {
  try {
    return f();
  } catch (e) {
    // ignore
  }
}

Written well:

js
export function read(f) {
  try {
    return f();
  } catch (e) {
    if (e.code !== "ENOENT") throw e;
  }
}

Caught as swallowed-error:

go
package ad

func Load(get func() error) error {
	if err := get(); err != nil {
		return nil
	}
	return nil
}

func Save(put func() error) error {
	err := put()
	if err != nil {
		return nil
	}
	return nil
}

Written well:

go
package ad

func Save(put func() error) error {
	err := put()
	if err != nil {
		return err
	}
	return nil
}

The other patterns in Errors and checks hidden:

Released under the Apache-2.0 license. · llms.txt