github.com/GeniusesGroup/libgo@v0.0.0-20220929090155-5ff932cb408e/error/errors.go (about) 1 /* For license and copyright information please see the LEGAL file in the code repository */ 2 3 package error 4 5 import ( 6 "github.com/GeniusesGroup/libgo/protocol" 7 ) 8 9 // Errors store 10 type Errors struct { 11 poolByID map[protocol.MediaTypeID]protocol.Error 12 poolByMediaType map[string]protocol.Error 13 } 14 15 func (e *Errors) Init() { 16 e.poolByID = make(map[protocol.MediaTypeID]protocol.Error, 512) 17 e.poolByMediaType = make(map[string]protocol.Error, 512) 18 } 19 20 func (e *Errors) RegisterError(err protocol.Error) { 21 var errID = err.ID() 22 23 if errID == 0 { 24 // This condition will just be true in the dev phase. 25 panic("Error must have valid ID to save it in platform errors pools. Initialize inner e.MediaType.Init() first if use libgo/service package.") 26 } 27 28 if protocol.AppMode_Dev && e.poolByID[errID] != nil { 29 // This condition will just be true in the dev phase. 30 panic("Error id exist and used for other Error. Check it now for bad media-type set or collision occurred" + 31 "\nExiting error >> " + e.poolByID[errID].ToString() + 32 "\nNew error >> " + err.ToString()) 33 } 34 35 e.poolByID[errID] = err 36 e.poolByMediaType[err.ToString()] = err 37 } 38 39 func (e *Errors) UnRegisterError(err protocol.Error) { 40 delete(e.poolByID, err.ID()) 41 delete(e.poolByMediaType, err.ToString()) 42 } 43 44 // GetErrorByID returns desire error if exist or ErrNotFound! 45 func (e *Errors) GetErrorByID(id protocol.MediaTypeID) (err protocol.Error) { 46 if id == 0 { 47 return 48 } 49 var ok bool 50 err, ok = e.poolByID[id] 51 if !ok { 52 err = &ErrNotFound 53 } 54 return 55 } 56 57 // GetErrorByMediaType returns desire error if exist or ErrNotFound! 58 func (e *Errors) GetErrorByMediaType(mt string) (err protocol.Error) { 59 var ok bool 60 err, ok = e.poolByMediaType[mt] 61 if !ok { 62 err = &ErrNotFound 63 } 64 return 65 }