errors.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package handlers
  15. import (
  16. "context"
  17. "net/http"
  18. "runtime/debug"
  19. "yunion.io/x/log"
  20. "yunion.io/x/pkg/appctx"
  21. "yunion.io/x/pkg/errors"
  22. "yunion.io/x/s3cli"
  23. "yunion.io/x/onecloud/pkg/appsrv"
  24. "yunion.io/x/onecloud/pkg/httperrors"
  25. )
  26. func generalError(ctx context.Context, statusCode int, errCode string, msg string) s3cli.ErrorResponse {
  27. o := fetchObjectRequest(ctx)
  28. resp := s3cli.ErrorResponse{}
  29. resp.StatusCode = statusCode
  30. resp.Code = errCode
  31. resp.Message = msg
  32. resp.BucketName = o.Bucket
  33. resp.Key = o.Key
  34. resp.HostID = appctx.AppContextHostId(ctx)
  35. resp.RequestID = appctx.AppContextRequestId(ctx)
  36. return resp
  37. }
  38. func BadRequest(ctx context.Context, msg string) s3cli.ErrorResponse {
  39. return generalError(ctx, 400, "Bad Request", msg)
  40. }
  41. func Unauthenticated(ctx context.Context, msg string) s3cli.ErrorResponse {
  42. return generalError(ctx, 401, "Unauthenticated", msg)
  43. }
  44. func Unauthorized(ctx context.Context, msg string) s3cli.ErrorResponse {
  45. return generalError(ctx, 403, "Unauthorized", msg)
  46. }
  47. func Forbidden(ctx context.Context, msg string) s3cli.ErrorResponse {
  48. return generalError(ctx, 403, "Forbidden", msg)
  49. }
  50. func NotFound(ctx context.Context, msg string) s3cli.ErrorResponse {
  51. return generalError(ctx, 404, "Not Found", msg)
  52. }
  53. func NotSupported(ctx context.Context, msg string) s3cli.ErrorResponse {
  54. return generalError(ctx, 404, "Not Supported", msg)
  55. }
  56. func NotImplemented(ctx context.Context, msg string) s3cli.ErrorResponse {
  57. return generalError(ctx, 406, "Not Implemented", msg)
  58. }
  59. func InvalidStatus(ctx context.Context, msg string) s3cli.ErrorResponse {
  60. return generalError(ctx, 406, "Invalid Status", msg)
  61. }
  62. func Conflict(ctx context.Context, msg string) s3cli.ErrorResponse {
  63. return generalError(ctx, 409, "Conflict", msg)
  64. }
  65. func ServerTimeout(ctx context.Context, msg string) s3cli.ErrorResponse {
  66. return generalError(ctx, 504, "Server Timeout", msg)
  67. }
  68. func ServerError(ctx context.Context, msg string) s3cli.ErrorResponse {
  69. return generalError(ctx, 500, "Internal Server Error", msg)
  70. }
  71. func OutOfRangeError(ctx context.Context, msg string) s3cli.ErrorResponse {
  72. return generalError(ctx, 416, "Range Not Satisfiable", msg)
  73. }
  74. func SendGeneralError(ctx context.Context, w http.ResponseWriter, err error) {
  75. switch e := err.(type) {
  76. case s3cli.ErrorResponse:
  77. SendError(ctx, w, e)
  78. case *s3cli.ErrorResponse:
  79. SendError(ctx, w, *e)
  80. default:
  81. var eresp s3cli.ErrorResponse
  82. cause := errors.Cause(err)
  83. switch cause {
  84. case httperrors.ErrUnauthenticated:
  85. eresp = Unauthenticated(ctx, err.Error())
  86. case httperrors.ErrUnauthorized:
  87. eresp = Unauthorized(ctx, err.Error())
  88. case httperrors.ErrNotFound:
  89. eresp = NotFound(ctx, err.Error())
  90. case httperrors.ErrNotSupported:
  91. eresp = NotSupported(ctx, err.Error())
  92. case httperrors.ErrNotImplemented:
  93. eresp = NotImplemented(ctx, err.Error())
  94. case httperrors.ErrDuplicateId:
  95. eresp = Conflict(ctx, err.Error())
  96. case httperrors.ErrTimeout:
  97. eresp = ServerTimeout(ctx, err.Error())
  98. case httperrors.ErrInvalidStatus:
  99. eresp = InvalidStatus(ctx, err.Error())
  100. case httperrors.ErrBadRequest:
  101. eresp = BadRequest(ctx, err.Error())
  102. case httperrors.ErrOutOfRange:
  103. eresp = OutOfRangeError(ctx, err.Error())
  104. case httperrors.ErrForbidden:
  105. eresp = Forbidden(ctx, err.Error())
  106. default:
  107. eresp = ServerError(ctx, err.Error())
  108. }
  109. SendError(ctx, w, eresp)
  110. }
  111. }
  112. func SendError(ctx context.Context, w http.ResponseWriter, resp s3cli.ErrorResponse) {
  113. w.WriteHeader(resp.StatusCode)
  114. log.Errorf("SendError: %s", resp)
  115. debug.PrintStack()
  116. appsrv.SendXml(w, nil, resp)
  117. }