Documentation
¶
Overview ¶
Package adapter holds the machinery every driver adapter under ./adapter shares: classify the driver's error into an errorx code, then build the AppError the same way every time.
An adapter supplies a Spec — what the resource field is called, an ordered list of classifiers, and the fallback for errors none of them recognize — and Spec.Handle does the rest:
var spec = adapter.Spec{
ResourceKey: "topic",
Classify: []adapter.ClassifyFunc{classifyProtocol, classifyNet},
Fallback: adapter.Classification{
Code: errorx.ErrExtEventBusPublishFailed,
Message: "kafka operation failed",
},
}
func HandleError(err error, topic string, extra map[string]any) *errorx.AppError {
return spec.Handle(err, topic, extra)
}
Classifiers should inspect typed errors — a driver's own error type, an errors.As target, a protocol code — not the text of err.Error(). Message text is not part of a driver's compatibility promise and changes between releases.
Index ¶
Constants ¶
const DriverErrorField = "driver_error"
DriverErrorField is the field an unclassified driver error's text is stored under. It is in Fields, which AppError does not serialize.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Classification ¶
type Classification struct {
// Code is the errorx code, e.g. errorx.ErrRepoSerialization. Required.
Code string
// Message is the internal developer message. It ends up in AppError.Message,
// which is never serialized to clients.
Message string
// Details is a human-readable explanation of what went wrong. It IS
// serialized to clients, so it must never carry driver text: a MySQL
// duplicate-key message quotes the offending value, and an auth failure
// quotes the user name. Write a fixed sentence here and let the driver's
// own words go into Fields, which is not serialized.
Details string
// Fields are classification-specific fields, e.g. {"error_type": "timeout"}.
// They are copied, never retained.
Fields map[string]any
}
Classification is the verdict a classifier reaches about a driver error: the errorx code it maps to, plus the context worth attaching.
type ClassifyFunc ¶
type ClassifyFunc func(err error) (c Classification, ok bool)
ClassifyFunc inspects a driver error and reports how to classify it. Return ok=false to decline, and the next classifier in the list gets a turn.
func Match ¶
func Match(pred func(error) bool, c Classification) ClassifyFunc
Match builds a ClassifyFunc from a predicate and a fixed classification. It covers the common shape where recognizing the error and describing it are two separate, stateless steps.
type Spec ¶
type Spec struct {
// ResourceKey names the field the resource argument is stored under:
// "key" for a cache, "table" for a database, "topic" for a broker.
// Empty means the resource argument is dropped.
ResourceKey string
// Classify is consulted in order; the first classifier that returns ok wins.
Classify []ClassifyFunc
// Fallback classifies any error no classifier claimed. Its Code is required.
Fallback Classification
}
Spec describes how one adapter turns its driver's errors into an AppError.
func (Spec) Handle ¶
Handle converts a driver error into an *errorx.AppError, or returns nil if err is nil. The original error stays wrapped, so errors.Is and errors.As still reach the driver's own sentinels.
extra is merged last and wins over the classification's own fields, so a caller can always override what the adapter guessed.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package adaptertest is the conformance suite every driver adapter runs against.
|
Package adaptertest is the conformance suite every driver adapter runs against. |
|
Package asynq converts task-queue failures into an errorx.AppError.
|
Package asynq converts task-queue failures into an errorx.AppError. |
|
Package clickhouse converts clickhouse-go failures into an errorx.AppError.
|
Package clickhouse converts clickhouse-go failures into an errorx.AppError. |
|
Package eventbus converts publish/subscribe failures into an errorx.AppError.
|
Package eventbus converts publish/subscribe failures into an errorx.AppError. |
|
Package firebase converts Firebase Cloud Messaging failures into an errorx.AppError.
|
Package firebase converts Firebase Cloud Messaging failures into an errorx.AppError. |
|
Package kafka converts segmentio/kafka-go failures into an errorx.AppError.
|
Package kafka converts segmentio/kafka-go failures into an errorx.AppError. |
|
Package minio converts MinIO object-storage failures into an errorx.AppError.
|
Package minio converts MinIO object-storage failures into an errorx.AppError. |
|
Package mongo converts mongo-driver failures into an errorx.AppError.
|
Package mongo converts mongo-driver failures into an errorx.AppError. |
|
Package mysql converts go-sql-driver/mysql failures into an errorx.AppError.
|
Package mysql converts go-sql-driver/mysql failures into an errorx.AppError. |
|
Package nats converts nats.go and JetStream failures into an errorx.AppError.
|
Package nats converts nats.go and JetStream failures into an errorx.AppError. |
|
Package errors contains PostgreSQL error codes constants.
|
Package errors contains PostgreSQL error codes constants. |
|
Package rabbitmq converts amqp091-go failures into an errorx.AppError.
|
Package rabbitmq converts amqp091-go failures into an errorx.AppError. |
|
Package redis converts go-redis failures into an errorx.AppError.
|
Package redis converts go-redis failures into an errorx.AppError. |
|
Package sql converts database/sql failures into an errorx.AppError without depending on any driver.
|
Package sql converts database/sql failures into an errorx.AppError without depending on any driver. |
|
Package telegram converts Telegram Bot API failures into an errorx.AppError.
|
Package telegram converts Telegram Bot API failures into an errorx.AppError. |