package push
import (
"errors"
"fmt"
)
const (
ReasonHandlerNil = "handler cannot be nil"
ReasonHandlerExists = "cannot overwrite existing handler"
ReasonHandlerProtected = "handler is protected"
ReasonPushNotificationsDisabled = "push notifications are disabled"
)
type ProcessorType string
const (
ProcessorTypeProcessor = ProcessorType ("processor" )
ProcessorTypeVoidProcessor = ProcessorType ("void_processor" )
ProcessorTypeCustom = ProcessorType ("custom" )
)
type ProcessorOperation string
const (
ProcessorOperationProcess = ProcessorOperation ("process" )
ProcessorOperationRegister = ProcessorOperation ("register" )
ProcessorOperationUnregister = ProcessorOperation ("unregister" )
ProcessorOperationUnknown = ProcessorOperation ("unknown" )
)
var (
ErrHandlerNil = errors .New (ReasonHandlerNil )
)
func ErrHandlerExists (pushNotificationName string ) error {
return NewHandlerError (ProcessorOperationRegister , pushNotificationName , ReasonHandlerExists , nil )
}
func ErrProtectedHandler (pushNotificationName string ) error {
return NewHandlerError (ProcessorOperationUnregister , pushNotificationName , ReasonHandlerProtected , nil )
}
func ErrVoidProcessorRegister (pushNotificationName string ) error {
return NewProcessorError (ProcessorTypeVoidProcessor , ProcessorOperationRegister , pushNotificationName , ReasonPushNotificationsDisabled , nil )
}
func ErrVoidProcessorUnregister (pushNotificationName string ) error {
return NewProcessorError (ProcessorTypeVoidProcessor , ProcessorOperationUnregister , pushNotificationName , ReasonPushNotificationsDisabled , nil )
}
type HandlerError struct {
Operation ProcessorOperation
PushNotificationName string
Reason string
Err error
}
func (e *HandlerError ) Error () string {
if e .Err != nil {
return fmt .Sprintf ("handler %s failed for '%s': %s (%v)" , e .Operation , e .PushNotificationName , e .Reason , e .Err )
}
return fmt .Sprintf ("handler %s failed for '%s': %s" , e .Operation , e .PushNotificationName , e .Reason )
}
func (e *HandlerError ) Unwrap () error {
return e .Err
}
func NewHandlerError (operation ProcessorOperation , pushNotificationName , reason string , err error ) *HandlerError {
return &HandlerError {
Operation : operation ,
PushNotificationName : pushNotificationName ,
Reason : reason ,
Err : err ,
}
}
type ProcessorError struct {
ProcessorType ProcessorType
Operation ProcessorOperation
PushNotificationName string
Reason string
Err error
}
func (e *ProcessorError ) Error () string {
notifInfo := ""
if e .PushNotificationName != "" {
notifInfo = fmt .Sprintf (" for '%s'" , e .PushNotificationName )
}
if e .Err != nil {
return fmt .Sprintf ("%s %s failed%s: %s (%v)" , e .ProcessorType , e .Operation , notifInfo , e .Reason , e .Err )
}
return fmt .Sprintf ("%s %s failed%s: %s" , e .ProcessorType , e .Operation , notifInfo , e .Reason )
}
func (e *ProcessorError ) Unwrap () error {
return e .Err
}
func NewProcessorError (processorType ProcessorType , operation ProcessorOperation , pushNotificationName , reason string , err error ) *ProcessorError {
return &ProcessorError {
ProcessorType : processorType ,
Operation : operation ,
PushNotificationName : pushNotificationName ,
Reason : reason ,
Err : err ,
}
}
func IsHandlerNilError (err error ) bool {
return errors .Is (err , ErrHandlerNil )
}
func IsHandlerExistsError (err error ) bool {
var handlerErr *HandlerError
if errors .As (err , &handlerErr ) {
return handlerErr .Operation == ProcessorOperationRegister && handlerErr .Reason == ReasonHandlerExists
}
return false
}
func IsProtectedHandlerError (err error ) bool {
var handlerErr *HandlerError
if errors .As (err , &handlerErr ) {
return handlerErr .Operation == ProcessorOperationUnregister && handlerErr .Reason == ReasonHandlerProtected
}
return false
}
func IsVoidProcessorError (err error ) bool {
var procErr *ProcessorError
if errors .As (err , &procErr ) {
return procErr .ProcessorType == ProcessorTypeVoidProcessor && procErr .Reason == ReasonPushNotificationsDisabled
}
return false
}
The pages are generated with Golds v0.8.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .