| 
									
										
										
										
											2024-01-21 21:40:06 -06:00
										 |  |  | package handler | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"errors" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	rErrors "codeberg.org/danjones000/responsable-errors" | 
					
						
							|  |  |  | 	"github.com/gin-gonic/gin" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type config struct { | 
					
						
							| 
									
										
										
										
											2024-01-22 10:29:58 -06:00
										 |  |  | 	loggers      []LoggerFunc | 
					
						
							| 
									
										
										
										
											2024-01-21 21:40:06 -06:00
										 |  |  | 	transformers []Transformer | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-22 20:08:03 -06:00
										 |  |  | // A function which should be used to log the responded error. | 
					
						
							| 
									
										
										
										
											2024-01-22 10:29:58 -06:00
										 |  |  | type LoggerFunc func(*gin.Context, rErrors.ResponsableError) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-22 20:08:03 -06:00
										 |  |  | // A function which should potentially return a [rErrors.ResponsableError] which wraps the provided error. | 
					
						
							| 
									
										
										
										
											2024-01-21 21:40:06 -06:00
										 |  |  | type Transformer func(error) rErrors.ResponsableError | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-22 20:08:03 -06:00
										 |  |  | // An option that customizes the behavior of [ErrorMiddleware] | 
					
						
							| 
									
										
										
										
											2024-01-21 21:40:06 -06:00
										 |  |  | type Option func(config) config | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-22 20:08:03 -06:00
										 |  |  | // Provides an [Option] which adds the specified [LoggerFunc] to the [ErrorMiddleware] | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // Multiple [LoggerFunc] may be added. | 
					
						
							| 
									
										
										
										
											2024-01-22 10:29:58 -06:00
										 |  |  | func WithLogger(logger LoggerFunc) Option { | 
					
						
							|  |  |  | 	return func(c config) config { | 
					
						
							|  |  |  | 		c.loggers = append(c.loggers, logger) | 
					
						
							|  |  |  | 		return c | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-22 20:08:03 -06:00
										 |  |  | // Provides an [Option] which adds the specified [Transformer] to the [ErrorMiddleware] | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // Multiple [Transformer] may (and probably should) be added. | 
					
						
							| 
									
										
										
										
											2024-01-21 21:40:06 -06:00
										 |  |  | func WithTransformer(tr Transformer) Option { | 
					
						
							|  |  |  | 	return func(c config) config { | 
					
						
							|  |  |  | 		c.transformers = append(c.transformers, tr) | 
					
						
							|  |  |  | 		return c | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-22 20:08:03 -06:00
										 |  |  | // Provides an [Option] which adds the default [Transformer] to the [ErrorMiddleware] | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // This [Transformer] handles a [gin.Error]. | 
					
						
							| 
									
										
										
										
											2024-01-21 21:40:06 -06:00
										 |  |  | func WithDefaultTransformer() Option { | 
					
						
							|  |  |  | 	return WithTransformer(ginTransformer) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func ginTransformer(er error) rErrors.ResponsableError { | 
					
						
							|  |  |  | 	var err *gin.Error | 
					
						
							|  |  |  | 	if !errors.As(er, &err) { | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	switch err.Type { | 
					
						
							|  |  |  | 	case gin.ErrorTypePrivate: | 
					
						
							|  |  |  | 		return rErrors.NewInternalError("%w", err) | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 		return rErrors.NewBadRequest("%w", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |