feat: initial version
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
package presenter
|
||||
|
||||
import "errors"
|
||||
|
||||
type (
|
||||
Code string
|
||||
Entity string
|
||||
)
|
||||
|
||||
const (
|
||||
CodeConflict = Code("CONFLICT")
|
||||
CodeNotFound = Code("NOT_FOUND")
|
||||
CodePreconditionFailed = Code("PRECONDITION_FAILED")
|
||||
)
|
||||
|
||||
const (
|
||||
EntityCompany = Entity("COMPANY")
|
||||
EntityConsumer = Entity("CONSUMER")
|
||||
EntityAddress = Entity("ADDRESS")
|
||||
EntityContactPerson = Entity("CONTACT_PERSON")
|
||||
EntityActivity = Entity("ACTIVITY")
|
||||
EntityPrice = Entity("PRICE")
|
||||
EntityEmployee = Entity("EMPLOYEE")
|
||||
EntityContract = Entity("CONTRACT")
|
||||
EntityEmploymentAgreement = Entity("EMPLOYMENT_AGREEMENT")
|
||||
EntityInvoiceBasis = Entity("INVOICE_BASIS")
|
||||
EntityInvoice = Entity("INVOICE")
|
||||
EntitySupplier = Entity("SUPPLIER")
|
||||
EntitySupplierInvoice = Entity("SUPPLIER_INVOICE")
|
||||
EntityTimeEntry = Entity("TIME_ENTRY")
|
||||
EntityEntrySeries = Entity("ENTRY_SERIES")
|
||||
EntityFiscalYear = Entity("FISCAL_YEAR")
|
||||
EntityAccountClass = Entity("ACCOUNT_CLASS")
|
||||
EntityAccountGroup = Entity("ACCOUNT_GROUP")
|
||||
EntityAccount = Entity("ACCOUNT")
|
||||
EntityTag = Entity("TAG")
|
||||
EntityEntry = Entity("ENTRY")
|
||||
EntityEntryBasis = Entity("ENTRY_BASIS")
|
||||
)
|
||||
|
||||
type CodedError struct {
|
||||
Message string
|
||||
Code Code
|
||||
Entity Entity
|
||||
Params map[string]string
|
||||
}
|
||||
|
||||
func NewCodedError(message string, code Code, entity Entity) CodedError {
|
||||
return CodedError{
|
||||
Message: message,
|
||||
Code: code,
|
||||
Entity: entity,
|
||||
Params: make(map[string]string),
|
||||
}
|
||||
}
|
||||
|
||||
func (c CodedError) Error() string {
|
||||
return c.Message
|
||||
}
|
||||
|
||||
func (c CodedError) Is(err error) bool {
|
||||
var e CodedError
|
||||
if errors.As(err, &e) {
|
||||
return c.Code == e.Code && c.Entity == e.Entity && c.Message == e.Message && mapEqual(c.Params, e.Params)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func mapEqual(m1, m2 map[string]string) bool {
|
||||
if len(m1) != len(m2) {
|
||||
return false
|
||||
}
|
||||
for k1, v1 := range m1 {
|
||||
v2, exists := m2[k1]
|
||||
if !exists || v1 != v2 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (c CodedError) WithParam(key, value string) CodedError {
|
||||
params := make(map[string]string)
|
||||
for k, v := range c.Params {
|
||||
params[k] = v
|
||||
}
|
||||
params[key] = value
|
||||
return CodedError{
|
||||
Message: c.Message,
|
||||
Code: c.Code,
|
||||
Entity: c.Entity,
|
||||
Params: params,
|
||||
}
|
||||
}
|
||||
|
||||
var _ error = CodedError{}
|
||||
Reference in New Issue
Block a user