40 lines
		
	
	
		
			647 B
		
	
	
	
		
			Go
		
	
	
		
		
			
		
	
	
			40 lines
		
	
	
		
			647 B
		
	
	
	
		
			Go
		
	
	
| 
								 | 
							
								package errors
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								import (
							 | 
						||
| 
								 | 
							
									"encoding/json"
							 | 
						||
| 
								 | 
							
									"fmt"
							 | 
						||
| 
								 | 
							
								)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								type Error struct {
							 | 
						||
| 
								 | 
							
									Code    int    `json:"code"`
							 | 
						||
| 
								 | 
							
									ID      string `json:"id"`
							 | 
						||
| 
								 | 
							
									Message string `json:"msg"`
							 | 
						||
| 
								 | 
							
									Tmpl    string `json:"-"`
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								func (e Error) New(v ...interface{}) Error {
							 | 
						||
| 
								 | 
							
									e.Message = fmt.Sprintf(e.Tmpl, v...)
							 | 
						||
| 
								 | 
							
									return e
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								func (e Error) Error() string {
							 | 
						||
| 
								 | 
							
									return e.Message
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								func (e Error) String() string {
							 | 
						||
| 
								 | 
							
									return e.Message
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								func (e Error) Json() []byte {
							 | 
						||
| 
								 | 
							
									data, _ := json.Marshal(e)
							 | 
						||
| 
								 | 
							
									return data
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								func New(code int, err string) *Error {
							 | 
						||
| 
								 | 
							
									return &Error{Code: code, Message: err}
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								func NewError(code int, err error) *Error {
							 | 
						||
| 
								 | 
							
									return &Error{Code: code, Message: err.Error()}
							 | 
						||
| 
								 | 
							
								}
							 |