Rules Hub
Coding Rules Library
Include context in error logs
Always include relevant identifiers (IDs, slugs, keys) in error messages to facilitate debugging and reproduction.
Bad example
| 1 | const fetchProduct = async (productId: string) => { |
| 2 | try { |
| 3 | return await api.getProduct(productId); |
| 4 | } catch (error) { |
| 5 | // Generic error message makes it hard to know WHICH product failed |
| 6 | console.error('Error fetching product details:', error); |
| 7 | return null; |
| 8 | } |
| 9 | }; |
Explanation (EN)
The error message is generic. If this error appears in logs, you won't know which specific product ID caused the failure, making reproduction difficult.
Objašnjenje (HR)
Poruka o pogrešci je generička. Ako se ova greška pojavi u zapisima (logovima), nećete znati koji je specifičan ID proizvoda uzrokovao problem, što otežava reprodukciju.
Good example
| 1 | const fetchProduct = async (productId: string) => { |
| 2 | try { |
| 3 | return await api.getProduct(productId); |
| 4 | } catch (error) { |
| 5 | // Includes the context (productId) directly in the log message |
| 6 | console.error(`Error fetching product details for ID ${productId}:`, error); |
| 7 | return null; |
| 8 | } |
| 9 | }; |
Explanation (EN)
The error message includes the specific `productId`. This context is crucial for debugging, as it allows developers to immediately identify and test the failing entity.
Objašnjenje (HR)
Poruka o pogrešci uključuje specifičan `productId`. Ovaj kontekst je ključan za debugging jer omogućuje programerima da odmah identificiraju i testiraju entitet koji je uzrokovao problem.