Files
tm_back/pkg/notification/errors.go
T
n.nakhostin 108629278a Integrate Notification Service into Tender Management System
- Added a new notification service to handle various notification types (email, SMS, push, OTP).
- Implemented configuration for the notification service, allowing customization via environment variables.
- Updated user and customer services to utilize the notification service for sending welcome emails and status updates.
- Introduced a builder pattern for constructing notification requests, enhancing usability and flexibility.
- Enhanced error handling and logging for notification operations, ensuring robust service integration.
- Updated documentation to include usage examples and configuration details for the new notification SDK.
2025-09-16 15:35:36 +03:30

84 lines
2.7 KiB
Go

package notification
import (
"fmt"
"net/http"
)
// Error types for notification SDK
var (
ErrNotificationServiceUnavailable = fmt.Errorf("notification service unavailable")
ErrInvalidRequest = fmt.Errorf("invalid notification request")
ErrNetworkTimeout = fmt.Errorf("network timeout")
ErrUnauthorized = fmt.Errorf("unauthorized request")
ErrInternalError = fmt.Errorf("internal notification service error")
)
// ErrInvalidConfig represents configuration validation errors
type ErrInvalidConfig struct {
Field string
Message string
}
func (e ErrInvalidConfig) Error() string {
return fmt.Sprintf("invalid config field %s: %s", e.Field, e.Message)
}
// ErrHTTP represents HTTP-related errors with status codes
type ErrHTTP struct {
StatusCode int
Message string
Body string
}
func (e ErrHTTP) Error() string {
return fmt.Sprintf("HTTP %d: %s", e.StatusCode, e.Message)
}
// ErrValidation represents validation errors
type ErrValidation struct {
Field string
Message string
}
func (e ErrValidation) Error() string {
return fmt.Sprintf("validation error for field %s: %s", e.Field, e.Message)
}
// ErrRetryExhausted represents retry exhaustion errors
type ErrRetryExhausted struct {
Attempts int
LastError error
ElapsedTime string
}
func (e ErrRetryExhausted) Error() string {
return fmt.Sprintf("retry exhausted after %d attempts (%s): %v", e.Attempts, e.ElapsedTime, e.LastError)
}
// MapHTTPError maps HTTP status codes to appropriate errors
func MapHTTPError(statusCode int, body string) error {
switch statusCode {
case http.StatusBadRequest:
return ErrHTTP{StatusCode: statusCode, Message: "bad request", Body: body}
case http.StatusUnauthorized:
return ErrHTTP{StatusCode: statusCode, Message: "unauthorized", Body: body}
case http.StatusForbidden:
return ErrHTTP{StatusCode: statusCode, Message: "forbidden", Body: body}
case http.StatusNotFound:
return ErrHTTP{StatusCode: statusCode, Message: "not found", Body: body}
case http.StatusUnprocessableEntity:
return ErrHTTP{StatusCode: statusCode, Message: "validation error", Body: body}
case http.StatusInternalServerError:
return ErrHTTP{StatusCode: statusCode, Message: "internal server error", Body: body}
case http.StatusBadGateway:
return ErrHTTP{StatusCode: statusCode, Message: "bad gateway", Body: body}
case http.StatusServiceUnavailable:
return ErrHTTP{StatusCode: statusCode, Message: "service unavailable", Body: body}
case http.StatusGatewayTimeout:
return ErrHTTP{StatusCode: statusCode, Message: "gateway timeout", Body: body}
default:
return ErrHTTP{StatusCode: statusCode, Message: "unknown error", Body: body}
}
}