Error messages should be specific, contextual, and use appropriate error creation functions to help users understand what went wrong and how to fix it.
Error messages should be specific, contextual, and use appropriate error creation functions to help users understand what went wrong and how to fix it.
Key principles:
Include relevant context: Error messages should contain specific identifiers, values, or context that help identify the source of the problem.
errors.New()
for static error messages without formattingfmt.Errorf()
only when you need to include dynamic valuestrace.BadParameter()
when appropriateExamples:
// Bad: Generic error without context
return trace.BadParameter("target is not an IC plugin")
// Good: Specific error with context
return trace.BadParameter("%q is not an AWS Identity Center integration", p.edit.awsIC.pluginName)
// Bad: Using fmt.Errorf for static messages
return fmt.Errorf("end time before start time")
// Good: Using errors.New for static messages
return errors.New("end time before start time")
// Good: Using fmt.Errorf when formatting is needed
return fmt.Errorf("invalid request size: expected %d bytes, got %d bytes", requestHeaderSize, len(data))
This approach improves debugging experience by providing actionable information that helps users identify and resolve issues quickly.
Enter the URL of a public GitHub repository