Rules Hub
Coding Rules Library
← Back to all rules
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
backend ruleP1universalStack: JavaScript
error-handlingrobustness
Match errors by type, not by message string
Branch on `instanceof` a known error class (or a discriminant field), never on `error.message.includes(...)`. Message text is unstable, locale-dependent and owned by third-party libs, so string matching silently breaks and misclassifies unrelated errors.
PR: profico-org-corpus batch2 (all stacks/products)Created: Jul 26, 2026
Bad example
Old codets
| 1 | try { parse(body); } catch (error) { |
| 2 | if (error.message.includes('JSON')) return res.status(400).end(); |
| 3 | throw error; |
| 4 | } |
Explanation (EN)
A library wording change, or any other error whose message happens to contain 'JSON', silently changes the branch taken.
Objašnjenje (HR)
Good example
New codets
| 1 | try { parse(body); } catch (error) { |
| 2 | if (error instanceof SyntaxError) return res.status(400).end(); |
| 3 | throw error; |
| 4 | } |
Explanation (EN)
Matching the error type is stable and unambiguous.
Objašnjenje (HR)