package errs import ( "fmt" "runtime" "github.com/davecgh/go-spew/spew" ) type ErroNil *Error type Map map[string]interface{} type Error struct { HttpStatus int `json:"httpStatus"` CodeText string `json:"codetext"` Message string `json:"message"` Stack *ErrorStack `json:"stack,omitempty"` Errors []*Detail `json:"erros,omitempty"` } type Detail struct { Dominio string `json:"dominio,omitempty"` Reason string `json:"reason,omitempty"` Message string `json:"message,omitempty"` LocationType string `json:"locationType,omitempty"` Location string `json:"location,omitempty"` } type ErrorMapFunction func(error) (err *Error) // type ErrorMapFunction func(error) (err error, replaced bool) var ( errorMapFunction = map[string]ErrorMapFunction{} ) func RegisterMapErrorFunction(id string, fn ErrorMapFunction) { errorMapFunction[id] = fn } // Adiciona uma descricao ao erro // func (e *Erro) Add(desc *Detail) *Erro { // e.Errors = append(e.Errors, desc) // return e // } func (e *Error) Details(desc *Detail) *Error { e.Errors = append(e.Errors, desc) if len(e.Errors) == 1 { e.Message = desc.Message } return e } // Retorna a ultima descricao adicionada func (e *Error) LastDescription() *Detail { size := len(e.Errors) if size > 0 { return e.Errors[size-1] } return nil } // Error retorna a mensagem de erro principal func (e *Error) Error() string { reason := "" if len(e.Errors) > 0 { reason = e.LastDescription().Reason } return fmt.Sprintf("[%s] %s",reason, e.Message) } // The operation was cancelled, typically by the caller. // // HTTP Mapping: 499 Client Closed Request func Cancelled() *Error { return errorHandler(&Error{ HttpStatus: 499, CodeText: "client_closed_request", Message: `Solicitação cancelada pelo cliente.`, }) } // Unknown error. For example, this error may be returned when // a `Status` value received from another address space belongs to // an error space that is not known in this address space. Also // errors raised by APIs that do not return enough error information // may be converted to this error. // // HTTP Mapping: 500 Internal Server Error func Unknown() *Error { return errorHandler(&Error{ HttpStatus: 500, CodeText: "internal_server_error", Message: "", }) } // The client specified an invalid argument. Note that this differs // from `FAILED_PRECONDITION`. `INVALID_ARGUMENT` indicates arguments // that are problematic regardless of the state of the system // (e.g., a malformed file name). // // HTTP Mapping: 400 Bad Request func InvalidArgument() *Error { return errorHandler(&Error{ HttpStatus: 400, CodeText: "bad_request", Message: "O campo de solicitação xyz é xxx, mas o esperado é [yyy, zzz].", }) } // The deadline expired before the operation could complete. For operations // that change the state of the system, this error may be returned // even if the operation has completed successfully. For example, a // successful response from a server could have been delayed long // enough for the deadline to expire. // // HTTP Mapping: 504 Gateway Timeout func DeadlineExceeded() *Error { return errorHandler(&Error{ HttpStatus: 504, CodeText: "gateway_timeout", Message: "", }) } // Some requested entity (e.g., file or directory) was not found. // // Note to server developers: if a request is denied for an entire class // of users, such as gradual feature rollout or undocumented whitelist, // `NOT_FOUND` may be used. If a request is denied for some users within // a class of users, such as user-based access control, `PERMISSION_DENIED` // must be used. // // HTTP Mapping: 404 Not Found func NotFound() *Error { return errorHandler(&Error{ HttpStatus: 404, CodeText: "not_found", Message: `O recurso "xxx" não foi encontrado.`, }) } // The entity that a client attempted to create (e.g., file or directory) // already exists. // // HTTP Mapping: 409 Conflict func AlreadyExists() *Error { return errorHandler(&Error{ HttpStatus: 409, CodeText: "conflict", Message: `O recurso já existe.`, }) } // The caller does not have permission to execute the specified // operation. `PERMISSION_DENIED` must not be used for rejections // caused by exhausting some resource (use `RESOURCE_EXHAUSTED` // instead for those errors). `PERMISSION_DENIED` must not be // used if the caller can not be identified (use `UNAUTHENTICATED` // instead for those errors). This error code does not imply the // request is valid or the requested entity exists or satisfies // other pre-conditions. // // HTTP Mapping: 403 Forbidden func PermissionDenied() *Error { return errorHandler(&Error{ HttpStatus: 403, CodeText: "forbidden", Message: `Permissão "xxx" negada no arquivo "yyy".`, }) } // The request does not have valid authentication credentials for the // operation. // // HTTP Mapping: 401 Unauthorized func Unauthenticated() *Error { return errorHandler(&Error{ HttpStatus: 401, CodeText: "unauthorized", Message: `Credenciais de autenticação inválidas.`, }) } // Some resource has been exhausted, perhaps a per-user quota, or // perhaps the entire file system is out of space. // // HTTP Mapping: 429 Too Many Requests func ResourceExhausted() *Error { return errorHandler(&Error{ HttpStatus: 429, CodeText: "too_many_requests", Message: `Limite de cota "xxx" excedido.`, }) } // The operation was rejected because the system is not in a state // required for the operation's execution. For example, the directory // to be deleted is non-empty, an rmdir operation is applied to // a non-directory, etc. // // Service implementors can use the following guidelines to decide // between `FAILED_PRECONDITION`, `ABORTED`, and `UNAVAILABLE`: // (a) Use `UNAVAILABLE` if the client can retry just the failing call. // (b) Use `ABORTED` if the client should retry at a higher level // (e.g., when a client-specified test-and-set fails, indicating the // client should restart a read-modify-write sequence). // (c) Use `FAILED_PRECONDITION` if the client should not retry until // the system state has been explicitly fixed. E.g., if an "rmdir" // fails because the directory is non-empty, `FAILED_PRECONDITION` // should be returned since the client should not retry unless // the files are deleted from the directory. // // HTTP Mapping: 400 Bad Request func FailedPrecondition() *Error { return errorHandler(&Error{ HttpStatus: 400, CodeText: "bad_request", Message: `O recurso xxx é um diretório que não está vazio, portanto, não pode ser excluído.`, }) } // The operation was aborted, typically due to a concurrency issue such as // a sequencer check failure or transaction abort. // // See the guidelines above for deciding between `FAILED_PRECONDITION`, // `ABORTED`, and `UNAVAILABLE`. // // HTTP Mapping: 409 Conflict func Aborted() *Error { return errorHandler(&Error{ HttpStatus: 409, CodeText: "conflict", Message: `Não foi possível adquirir o bloqueio no recurso "xxx".`, }) } // The operation was attempted past the valid range. E.g., seeking or // reading past end-of-file. // // Unlike `INVALID_ARGUMENT`, this error indicates a problem that may // be fixed if the system state changes. For example, a 32-bit file // system will generate `INVALID_ARGUMENT` if asked to read at an // offset that is not in the range [0,2^32-1], but it will generate // `OUT_OF_RANGE` if asked to read from an offset past the current // file size. // // There is a fair bit of overlap between `FAILED_PRECONDITION` and // `OUT_OF_RANGE`. We recommend using `OUT_OF_RANGE` (the more specific // error) when it applies so that callers who are iterating through // a space can easily look for an `OUT_OF_RANGE` error to detect when // they are done. // // HTTP Mapping: 400 Bad Request func OutOfRange() *Error { return errorHandler(&Error{ HttpStatus: 400, CodeText: "bad_request", Message: `A "idade" do parâmetro está fora do intervalo [0, 125].`, }) } func HTTPVersionNotSupported() *Error { return errorHandler(&Error{ HttpStatus: 400, CodeText: "http_version_not_supported", Message: `The server doesn’t support the major HTTP version the client used to make the request.`, }) } // The operation is not implemented or is not supported/enabled in this // service. // // HTTP Mapping: 501 Not Implemented func Unimplemented() *Error { return errorHandler(&Error{ HttpStatus: 501, CodeText: "not_implemented", Message: `Método "xxx" não implementado.`, }) } // Internal errors. This means that some invariants expected by the // underlying system have been broken. This error code is reserved // for serious errors. // // HTTP Mapping: 500 Internal Server Error func Internal() *Error { return errorHandler(&Error{ HttpStatus: 500, CodeText: "internal_server_error", Message: `Error interno.`, }) } // The service is currently unavailable. This is most likely a // transient condition, which can be corrected by retrying with // a backoff. // // See the guidelines above for deciding between `FAILED_PRECONDITION`, // `ABORTED`, and `UNAVAILABLE`. // // HTTP Mapping: 503 Service Unavailable func Unavaliable() *Error { return errorHandler(&Error{ HttpStatus: 503, CodeText: "service_unavailable", Message: "Serviço indisponivel.", }) } // Unrecoverable data loss or corruption. // // HTTP Mapping: 500 Internal Server Error func DataCaps() *Error { return errorHandler(&Error{ HttpStatus: 500, CodeText: "internal_server_error", Message: `Error interno.`, }) } func errorHandler(err *Error) *Error { err.Stack = Trace() return err } // } // func Error(code int, m string) *Error { // LogError(code, m) // e := ErrosList[code] // if m != "" { // e.Message = m // } // e.Details(&Detail{ // Dominio: "global", // Message: m, // }) // return &e // } // func FullError(code int, err *Detail) *Error { // // _, fn, line, _ := runtime.Caller(1 // // LogError(code, fmt.Sprintf("%s:%d - %s", fn, line, err.Message)) // e := ErrosList[code] // e.Message = err.Message // e.Stack = Trace() // e.Details(err) // return &e // } type ErrorStack struct { Frames []runtime.Frame } func (stack *ErrorStack) Print() { for _, f := range stack.Frames { fmt.Printf("%s\n\t%s:%d\n", f.Function, f.File, f.Line) } } func Trace() (stack *ErrorStack) { stack = &ErrorStack{} pc := make([]uintptr, 20) n := runtime.Callers(2, pc) if n == 0 { // No pcs available. Stop now. // This can happen if the first argument to runtime.Callers is large. return } pc = pc[:n] // pass only valid pcs to runtime.CallersFrames frames := runtime.CallersFrames(pc) // Loop to get frames. // A fixed number of pcs can expand to an indefinite number of Frames. for { frame, more := frames.Next() stack.Frames = append(stack.Frames, frame) // To keep this example's output stable // even if there are changes in the testing package, // stop unwinding when we leave package runtime. // if strings.Contains(frame.Function, "main.main") { // break // } // spew.Dump(frame) if !more { break } } return } func FromError(source error) (err *Error) { spew.Dump(source) for _, fn := range errorMapFunction { if err = fn(source); err != nil { return } } err = Internal().Details(&Detail{ Message: source.Error(), }) return } // 400 invalidParameter Indica que um parâmetro de solicitação tem um valor inválido. Os campos locationType e location na resposta de erro fornecem informações sobre qual valor era inválido. Não tente novamente sem corrigir o problema. Você precisa fornecer um valor válido para o parâmetro especificado na resposta de erro. // 400 badRequest Indica que a consulta era inválida. Por exemplo, o ID pai estava ausente ou a combinação de dimensões ou métricas solicitada não era válida. Não tente novamente sem corrigir o problema. Você precisa fazer alterações na consulta da API para que ela funcione. // 401 invalidCredentials Indica que o token de autenticação é inválido ou expirou. Não tente novamente sem corrigir o problema. Você precisa receber um novo token de autenticação. // 403 insufficientPermissions Indica que o usuário não tem permissões suficientes para a entidade especificada na consulta. Não tente novamente sem corrigir o problema. Você precisa receber permissões suficientes para executar a operação na entidade especificada. // 403 dailyLimitExceeded Indica que o usuário ultrapassou a cota diária [por projeto ou vista da propriedade (perfil)]. Não tente novamente sem corrigir o problema. Você esgotou sua cota diária. Consulte Limites e cotas da API. // 403 usageLimits.userRateLimitExceededUnreg Indica que o aplicativo precisa ser registrado no console de APIs do Google. Não tente novamente sem corrigir o problema. Você precisa se registrar no console de APIs para receber a cota completa da API. // 403 userRateLimitExceeded Indica que o limite da taxa de usuário foi ultrapassado. O limite de taxa máximo é de 10 qps por endereço IP. O valor padrão definido no console de APIs do Google é 1 qps por endereço IP. Você pode aumentar esse limite no console de APIs do Google para no máximo 10 qps. Tente novamente usando backoff exponencial. Você precisa diminuir a taxa em que envia as solicitações. // 403 rateLimitExceeded Indica que os limites de taxa global ou geral do projeto foram excedidos. Tente novamente usando backoff exponencial. Você precisa diminuir a taxa em que envia as solicitações. // 403 quotaExceeded Indica que o limite de 10 solicitações simultâneas por vista da propriedade (perfil) na API de relatórios principais foi atingido. Tente novamente usando backoff exponencial. Você precisa aguardar até que pelo menos uma solicitação em andamento para essa Vista (perfil) seja concluída. // ERR_BAD_REQUEST: Erro{ // HttpStatus: 400, // CodeText: "bad_request", // Message: "", // }, // ERR_INVALID_PARAM: Erro{ // HttpStatus: 400, // CodeText: "invalid_param", // Message: "", // }, // ERR_INVALID_PARAM_VALUE: Erro{ // HttpStatus: 400, // CodeText: "invalid_param_value", // Message: "", // }, // ERR_INVALID_CREDENTIAL: Erro{ // HttpStatus: 401, // CodeText: "invalid_credential", // Message: "Invalid Credentials", // }, // ERR_NOT_FOUND: Erro{ // HttpStatus: 404, // CodeText: "not_found", // Message: "Recurso não existe", // }, // ERR_PERSIST: Erro{ // HttpStatus: 500, // CodeText: "persist_error", // Message: "Não foi possivel salvar as alterações", // }, // ERR_CONFLICT: Erro{ // HttpStatus: 409, // CodeText: "conflict", // Message: "A operação falhou porque o item é referenciado por outras.", // }, // ERR_GENERAL: Erro{ // HttpStatus: 500, // CodeText: "general", // Message: "Parece que algo deu errado. Nós pedimos desculpas. Você pode voltar para a página principal", // }, // ERR_EXPIRED: Erro{ // HttpStatus: 500, // CodeText: "expired", // Message: "Parece que o recurso não esta mais disponível", // }, // const ( // ERR_NOT_FOUND int = iota // ERR_GENERAL // ERR_PERSIST // ERR_INVALID_CREDENTIAL // ERR_BAD_REQUEST // ERR_INVALID_PARAM // ERR_INVALID_PARAM_VALUE // ERR_EXPIRED // ERR_NOT_MODIFIED // ERR_TEMPORARY_REDIRECT // ERR_UNAUTHORIZED // ERR_PAYMENT_REQUIRED // ERR_FORBIDDEN // ERR_METHOD_NOT_ALLOWED // ERR_CONFLICT // ERR_TOO_MANY_REQUESTS // ERR_NOT_IMPLEMENTED // ERR_SERVICE_UNAVAILABLE // ) // Error cria uma instancia de um erro // code int assume: // ERR_NOT_FOUND int = 0 // ERR_GENERAL int = 1 // ERR_PERSIST int = 2 // ERR_INVALID_CREDENTIAL int = 3 // ERR_BAD_REQUEST int = 4 // ERR_INVALID_PARAM int = 5 // ERR_EXPIRED int = 6 // func Errorf(code int, m string, args ...interface{}) *Erro { // LogError(code, m) // m = fmt.Sprintf(m, args...) // e := ErrosList[code] // if m != "" { // e.Message = m // } // e.Add(&Detail{ // Dominio: "global", // Message: m, // }) // return &e // } // func Falha(ctx context.Context, code int, m string, dev Map) { // // fmt.Println("CodeText:[", code, "]", m, dev); // // ctx.StopExecution() // base := ErrosList[code] // base.Development = Map{} // if m != "" { // base.Message = m // } // if dev != nil { // base.Development = dev // } // accept := ctx.GetHeader("Accept") // ctx.StatusCode(base.Code) // //spew.Dump(base) // if base.Error == "not_found" { // url := ctx.Host() + ctx.RequestPath(true) // base.Development["url"] = fmt.Sprintf("A url '%s' não foi encontrada.", url) // } // if accept == "application/json" { // ctx.JSON(base) // } else { // ctx.ViewData("", base) // // ctx.View(ViewScript(ctx, "error.html")) // } // ctx.StopExecution() // }