errs.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. package errs
  2. import (
  3. "fmt"
  4. "runtime"
  5. "github.com/davecgh/go-spew/spew"
  6. )
  7. type ErroNil *Error
  8. type Map map[string]interface{}
  9. type Error struct {
  10. HttpStatus int `json:"httpStatus"`
  11. CodeText string `json:"codetext"`
  12. Message string `json:"message"`
  13. Stack *ErrorStack `json:"stack,omitempty"`
  14. Errors []*Detail `json:"erros,omitempty"`
  15. }
  16. type Detail struct {
  17. Dominio string `json:"dominio,omitempty"`
  18. Reason string `json:"reason,omitempty"`
  19. Message string `json:"message,omitempty"`
  20. LocationType string `json:"locationType,omitempty"`
  21. Location string `json:"location,omitempty"`
  22. }
  23. type ErrorMapFunction func(error) (err *Error)
  24. // type ErrorMapFunction func(error) (err error, replaced bool)
  25. var (
  26. errorMapFunction = map[string]ErrorMapFunction{}
  27. )
  28. func RegisterMapErrorFunction(id string, fn ErrorMapFunction) {
  29. errorMapFunction[id] = fn
  30. }
  31. // Adiciona uma descricao ao erro
  32. // func (e *Erro) Add(desc *Detail) *Erro {
  33. // e.Errors = append(e.Errors, desc)
  34. // return e
  35. // }
  36. func (e *Error) Details(desc *Detail) *Error {
  37. e.Errors = append(e.Errors, desc)
  38. if len(e.Errors) == 1 {
  39. e.Message = desc.Message
  40. }
  41. return e
  42. }
  43. // Retorna a ultima descricao adicionada
  44. func (e *Error) LastDescription() *Detail {
  45. size := len(e.Errors)
  46. if size > 0 {
  47. return e.Errors[size-1]
  48. }
  49. return nil
  50. }
  51. // Error retorna a mensagem de erro principal
  52. func (e *Error) Error() string {
  53. return e.Message
  54. }
  55. // The operation was cancelled, typically by the caller.
  56. //
  57. // HTTP Mapping: 499 Client Closed Request
  58. func Cancelled() *Error {
  59. return errorHandler(&Error{
  60. HttpStatus: 499,
  61. CodeText: "client_closed_request",
  62. Message: `Solicitação cancelada pelo cliente.`,
  63. })
  64. }
  65. // Unknown error. For example, this error may be returned when
  66. // a `Status` value received from another address space belongs to
  67. // an error space that is not known in this address space. Also
  68. // errors raised by APIs that do not return enough error information
  69. // may be converted to this error.
  70. //
  71. // HTTP Mapping: 500 Internal Server Error
  72. func Unknown() *Error {
  73. return errorHandler(&Error{
  74. HttpStatus: 500,
  75. CodeText: "internal_server_error",
  76. Message: "",
  77. })
  78. }
  79. // The client specified an invalid argument. Note that this differs
  80. // from `FAILED_PRECONDITION`. `INVALID_ARGUMENT` indicates arguments
  81. // that are problematic regardless of the state of the system
  82. // (e.g., a malformed file name).
  83. //
  84. // HTTP Mapping: 400 Bad Request
  85. func InvalidArgument() *Error {
  86. return errorHandler(&Error{
  87. HttpStatus: 400,
  88. CodeText: "bad_request",
  89. Message: "O campo de solicitação xyz é xxx, mas o esperado é [yyy, zzz].",
  90. })
  91. }
  92. // The deadline expired before the operation could complete. For operations
  93. // that change the state of the system, this error may be returned
  94. // even if the operation has completed successfully. For example, a
  95. // successful response from a server could have been delayed long
  96. // enough for the deadline to expire.
  97. //
  98. // HTTP Mapping: 504 Gateway Timeout
  99. func DeadlineExceeded() *Error {
  100. return errorHandler(&Error{
  101. HttpStatus: 504,
  102. CodeText: "gateway_timeout",
  103. Message: "",
  104. })
  105. }
  106. // Some requested entity (e.g., file or directory) was not found.
  107. //
  108. // Note to server developers: if a request is denied for an entire class
  109. // of users, such as gradual feature rollout or undocumented whitelist,
  110. // `NOT_FOUND` may be used. If a request is denied for some users within
  111. // a class of users, such as user-based access control, `PERMISSION_DENIED`
  112. // must be used.
  113. //
  114. // HTTP Mapping: 404 Not Found
  115. func NotFound() *Error {
  116. return errorHandler(&Error{
  117. HttpStatus: 404,
  118. CodeText: "not_found",
  119. Message: `O recurso "xxx" não foi encontrado.`,
  120. })
  121. }
  122. // The entity that a client attempted to create (e.g., file or directory)
  123. // already exists.
  124. //
  125. // HTTP Mapping: 409 Conflict
  126. func AlreadyExists() *Error {
  127. return errorHandler(&Error{
  128. HttpStatus: 409,
  129. CodeText: "conflict",
  130. Message: `O recurso já existe.`,
  131. })
  132. }
  133. // The caller does not have permission to execute the specified
  134. // operation. `PERMISSION_DENIED` must not be used for rejections
  135. // caused by exhausting some resource (use `RESOURCE_EXHAUSTED`
  136. // instead for those errors). `PERMISSION_DENIED` must not be
  137. // used if the caller can not be identified (use `UNAUTHENTICATED`
  138. // instead for those errors). This error code does not imply the
  139. // request is valid or the requested entity exists or satisfies
  140. // other pre-conditions.
  141. //
  142. // HTTP Mapping: 403 Forbidden
  143. func PermissionDenied() *Error {
  144. return errorHandler(&Error{
  145. HttpStatus: 403,
  146. CodeText: "forbidden",
  147. Message: `Permissão "xxx" negada no arquivo "yyy".`,
  148. })
  149. }
  150. // The request does not have valid authentication credentials for the
  151. // operation.
  152. //
  153. // HTTP Mapping: 401 Unauthorized
  154. func Unauthenticated() *Error {
  155. return errorHandler(&Error{
  156. HttpStatus: 401,
  157. CodeText: "unauthorized",
  158. Message: `Credenciais de autenticação inválidas.`,
  159. })
  160. }
  161. // Some resource has been exhausted, perhaps a per-user quota, or
  162. // perhaps the entire file system is out of space.
  163. //
  164. // HTTP Mapping: 429 Too Many Requests
  165. func ResourceExhausted() *Error {
  166. return errorHandler(&Error{
  167. HttpStatus: 429,
  168. CodeText: "too_many_requests",
  169. Message: `Limite de cota "xxx" excedido.`,
  170. })
  171. }
  172. // The operation was rejected because the system is not in a state
  173. // required for the operation's execution. For example, the directory
  174. // to be deleted is non-empty, an rmdir operation is applied to
  175. // a non-directory, etc.
  176. //
  177. // Service implementors can use the following guidelines to decide
  178. // between `FAILED_PRECONDITION`, `ABORTED`, and `UNAVAILABLE`:
  179. // (a) Use `UNAVAILABLE` if the client can retry just the failing call.
  180. // (b) Use `ABORTED` if the client should retry at a higher level
  181. // (e.g., when a client-specified test-and-set fails, indicating the
  182. // client should restart a read-modify-write sequence).
  183. // (c) Use `FAILED_PRECONDITION` if the client should not retry until
  184. // the system state has been explicitly fixed. E.g., if an "rmdir"
  185. // fails because the directory is non-empty, `FAILED_PRECONDITION`
  186. // should be returned since the client should not retry unless
  187. // the files are deleted from the directory.
  188. //
  189. // HTTP Mapping: 400 Bad Request
  190. func FailedPrecondition() *Error {
  191. return errorHandler(&Error{
  192. HttpStatus: 400,
  193. CodeText: "bad_request",
  194. Message: `O recurso xxx é um diretório que não está vazio, portanto, não pode ser excluído.`,
  195. })
  196. }
  197. // The operation was aborted, typically due to a concurrency issue such as
  198. // a sequencer check failure or transaction abort.
  199. //
  200. // See the guidelines above for deciding between `FAILED_PRECONDITION`,
  201. // `ABORTED`, and `UNAVAILABLE`.
  202. //
  203. // HTTP Mapping: 409 Conflict
  204. func Aborted() *Error {
  205. return errorHandler(&Error{
  206. HttpStatus: 409,
  207. CodeText: "conflict",
  208. Message: `Não foi possível adquirir o bloqueio no recurso "xxx".`,
  209. })
  210. }
  211. // The operation was attempted past the valid range. E.g., seeking or
  212. // reading past end-of-file.
  213. //
  214. // Unlike `INVALID_ARGUMENT`, this error indicates a problem that may
  215. // be fixed if the system state changes. For example, a 32-bit file
  216. // system will generate `INVALID_ARGUMENT` if asked to read at an
  217. // offset that is not in the range [0,2^32-1], but it will generate
  218. // `OUT_OF_RANGE` if asked to read from an offset past the current
  219. // file size.
  220. //
  221. // There is a fair bit of overlap between `FAILED_PRECONDITION` and
  222. // `OUT_OF_RANGE`. We recommend using `OUT_OF_RANGE` (the more specific
  223. // error) when it applies so that callers who are iterating through
  224. // a space can easily look for an `OUT_OF_RANGE` error to detect when
  225. // they are done.
  226. //
  227. // HTTP Mapping: 400 Bad Request
  228. func OutOfRange() *Error {
  229. return errorHandler(&Error{
  230. HttpStatus: 400,
  231. CodeText: "bad_request",
  232. Message: `A "idade" do parâmetro está fora do intervalo [0, 125].`,
  233. })
  234. }
  235. func HTTPVersionNotSupported() *Error {
  236. return errorHandler(&Error{
  237. HttpStatus: 400,
  238. CodeText: "http_version_not_supported",
  239. Message: `The server doesn’t support the major HTTP version the client used to make the request.`,
  240. })
  241. }
  242. // The operation is not implemented or is not supported/enabled in this
  243. // service.
  244. //
  245. // HTTP Mapping: 501 Not Implemented
  246. func Unimplemented() *Error {
  247. return errorHandler(&Error{
  248. HttpStatus: 501,
  249. CodeText: "not_implemented",
  250. Message: `Método "xxx" não implementado.`,
  251. })
  252. }
  253. // Internal errors. This means that some invariants expected by the
  254. // underlying system have been broken. This error code is reserved
  255. // for serious errors.
  256. //
  257. // HTTP Mapping: 500 Internal Server Error
  258. func Internal() *Error {
  259. return errorHandler(&Error{
  260. HttpStatus: 500,
  261. CodeText: "internal_server_error",
  262. Message: `Error interno.`,
  263. })
  264. }
  265. // The service is currently unavailable. This is most likely a
  266. // transient condition, which can be corrected by retrying with
  267. // a backoff.
  268. //
  269. // See the guidelines above for deciding between `FAILED_PRECONDITION`,
  270. // `ABORTED`, and `UNAVAILABLE`.
  271. //
  272. // HTTP Mapping: 503 Service Unavailable
  273. func Unavaliable() *Error {
  274. return errorHandler(&Error{
  275. HttpStatus: 503,
  276. CodeText: "service_unavailable",
  277. Message: "Serviço indisponivel.",
  278. })
  279. }
  280. // Unrecoverable data loss or corruption.
  281. //
  282. // HTTP Mapping: 500 Internal Server Error
  283. func DataCaps() *Error {
  284. return errorHandler(&Error{
  285. HttpStatus: 500,
  286. CodeText: "internal_server_error",
  287. Message: `Error interno.`,
  288. })
  289. }
  290. func errorHandler(err *Error) *Error {
  291. err.Stack = Trace()
  292. return err
  293. }
  294. // }
  295. // func Error(code int, m string) *Error {
  296. // LogError(code, m)
  297. // e := ErrosList[code]
  298. // if m != "" {
  299. // e.Message = m
  300. // }
  301. // e.Details(&Detail{
  302. // Dominio: "global",
  303. // Message: m,
  304. // })
  305. // return &e
  306. // }
  307. // func FullError(code int, err *Detail) *Error {
  308. // // _, fn, line, _ := runtime.Caller(1
  309. // // LogError(code, fmt.Sprintf("%s:%d - %s", fn, line, err.Message))
  310. // e := ErrosList[code]
  311. // e.Message = err.Message
  312. // e.Stack = Trace()
  313. // e.Details(err)
  314. // return &e
  315. // }
  316. type ErrorStack struct {
  317. Frames []runtime.Frame
  318. }
  319. func (stack *ErrorStack) Print() {
  320. for _, f := range stack.Frames {
  321. fmt.Printf("%s\n\t%s:%d\n", f.Function, f.File, f.Line)
  322. }
  323. }
  324. func Trace() (stack *ErrorStack) {
  325. stack = &ErrorStack{}
  326. pc := make([]uintptr, 20)
  327. n := runtime.Callers(2, pc)
  328. if n == 0 {
  329. // No pcs available. Stop now.
  330. // This can happen if the first argument to runtime.Callers is large.
  331. return
  332. }
  333. pc = pc[:n] // pass only valid pcs to runtime.CallersFrames
  334. frames := runtime.CallersFrames(pc)
  335. // Loop to get frames.
  336. // A fixed number of pcs can expand to an indefinite number of Frames.
  337. for {
  338. frame, more := frames.Next()
  339. stack.Frames = append(stack.Frames, frame)
  340. // To keep this example's output stable
  341. // even if there are changes in the testing package,
  342. // stop unwinding when we leave package runtime.
  343. // if strings.Contains(frame.Function, "main.main") {
  344. // break
  345. // }
  346. // spew.Dump(frame)
  347. if !more {
  348. break
  349. }
  350. }
  351. return
  352. }
  353. func FromError(source error) (err *Error) {
  354. spew.Dump(source)
  355. for _, fn := range errorMapFunction {
  356. if err = fn(source); err != nil {
  357. return
  358. }
  359. }
  360. err = Internal().Details(&Detail{
  361. Message: source.Error(),
  362. })
  363. return
  364. }
  365. // 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.
  366. // 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.
  367. // 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.
  368. // 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.
  369. // 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.
  370. // 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.
  371. // 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.
  372. // 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.
  373. // 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.
  374. // ERR_BAD_REQUEST: Erro{
  375. // HttpStatus: 400,
  376. // CodeText: "bad_request",
  377. // Message: "",
  378. // },
  379. // ERR_INVALID_PARAM: Erro{
  380. // HttpStatus: 400,
  381. // CodeText: "invalid_param",
  382. // Message: "",
  383. // },
  384. // ERR_INVALID_PARAM_VALUE: Erro{
  385. // HttpStatus: 400,
  386. // CodeText: "invalid_param_value",
  387. // Message: "",
  388. // },
  389. // ERR_INVALID_CREDENTIAL: Erro{
  390. // HttpStatus: 401,
  391. // CodeText: "invalid_credential",
  392. // Message: "Invalid Credentials",
  393. // },
  394. // ERR_NOT_FOUND: Erro{
  395. // HttpStatus: 404,
  396. // CodeText: "not_found",
  397. // Message: "Recurso não existe",
  398. // },
  399. // ERR_PERSIST: Erro{
  400. // HttpStatus: 500,
  401. // CodeText: "persist_error",
  402. // Message: "Não foi possivel salvar as alterações",
  403. // },
  404. // ERR_CONFLICT: Erro{
  405. // HttpStatus: 409,
  406. // CodeText: "conflict",
  407. // Message: "A operação falhou porque o item é referenciado por outras.",
  408. // },
  409. // ERR_GENERAL: Erro{
  410. // HttpStatus: 500,
  411. // CodeText: "general",
  412. // Message: "Parece que algo deu errado. Nós pedimos desculpas. Você pode voltar para a página principal",
  413. // },
  414. // ERR_EXPIRED: Erro{
  415. // HttpStatus: 500,
  416. // CodeText: "expired",
  417. // Message: "Parece que o recurso não esta mais disponível",
  418. // },
  419. // const (
  420. // ERR_NOT_FOUND int = iota
  421. // ERR_GENERAL
  422. // ERR_PERSIST
  423. // ERR_INVALID_CREDENTIAL
  424. // ERR_BAD_REQUEST
  425. // ERR_INVALID_PARAM
  426. // ERR_INVALID_PARAM_VALUE
  427. // ERR_EXPIRED
  428. // ERR_NOT_MODIFIED
  429. // ERR_TEMPORARY_REDIRECT
  430. // ERR_UNAUTHORIZED
  431. // ERR_PAYMENT_REQUIRED
  432. // ERR_FORBIDDEN
  433. // ERR_METHOD_NOT_ALLOWED
  434. // ERR_CONFLICT
  435. // ERR_TOO_MANY_REQUESTS
  436. // ERR_NOT_IMPLEMENTED
  437. // ERR_SERVICE_UNAVAILABLE
  438. // )
  439. // Error cria uma instancia de um erro
  440. // code int assume:
  441. // ERR_NOT_FOUND int = 0
  442. // ERR_GENERAL int = 1
  443. // ERR_PERSIST int = 2
  444. // ERR_INVALID_CREDENTIAL int = 3
  445. // ERR_BAD_REQUEST int = 4
  446. // ERR_INVALID_PARAM int = 5
  447. // ERR_EXPIRED int = 6
  448. // func Errorf(code int, m string, args ...interface{}) *Erro {
  449. // LogError(code, m)
  450. // m = fmt.Sprintf(m, args...)
  451. // e := ErrosList[code]
  452. // if m != "" {
  453. // e.Message = m
  454. // }
  455. // e.Add(&Detail{
  456. // Dominio: "global",
  457. // Message: m,
  458. // })
  459. // return &e
  460. // }
  461. // func Falha(ctx context.Context, code int, m string, dev Map) {
  462. // // fmt.Println("CodeText:[", code, "]", m, dev);
  463. // // ctx.StopExecution()
  464. // base := ErrosList[code]
  465. // base.Development = Map{}
  466. // if m != "" {
  467. // base.Message = m
  468. // }
  469. // if dev != nil {
  470. // base.Development = dev
  471. // }
  472. // accept := ctx.GetHeader("Accept")
  473. // ctx.StatusCode(base.Code)
  474. // //spew.Dump(base)
  475. // if base.Error == "not_found" {
  476. // url := ctx.Host() + ctx.RequestPath(true)
  477. // base.Development["url"] = fmt.Sprintf("A url '%s' não foi encontrada.", url)
  478. // }
  479. // if accept == "application/json" {
  480. // ctx.JSON(base)
  481. // } else {
  482. // ctx.ViewData("", base)
  483. // // ctx.View(ViewScript(ctx, "error.html"))
  484. // }
  485. // ctx.StopExecution()
  486. // }