Example_errorChaining demonstrates wrapping errors
()
| 170 | |
| 171 | // Example_errorChaining demonstrates wrapping errors |
| 172 | func Example_errorChaining() { |
| 173 | sql := "SELECT * FROM users" |
| 174 | location := models.Location{Line: 1, Column: 1} |
| 175 | |
| 176 | // Create a chain of errors |
| 177 | rootErr := errors.NewError( |
| 178 | errors.ErrCodeInvalidSyntax, |
| 179 | "invalid table reference", |
| 180 | location, |
| 181 | ) |
| 182 | |
| 183 | wrappedErr := errors.WrapError( |
| 184 | errors.ErrCodeUnexpectedToken, |
| 185 | "parser error while processing SELECT", |
| 186 | location, |
| 187 | sql, |
| 188 | rootErr, |
| 189 | ) |
| 190 | |
| 191 | fmt.Println("Chained Error:") |
| 192 | fmt.Println(wrappedErr.Error()) |
| 193 | fmt.Println() |
| 194 | |
| 195 | // Can unwrap to get root cause |
| 196 | if wrappedErr.Unwrap() != nil { |
| 197 | fmt.Println("Root cause available through Unwrap()") |
| 198 | } |
| 199 | |
| 200 | // Note: Output includes chained error with context |
| 201 | // Root cause available through Unwrap() |
| 202 | } |
| 203 | |
| 204 | // Example_customHintsEnhanced demonstrates adding custom hints to errors |
| 205 | func Example_customHintsEnhanced() { |