|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#include "iceberg/catalog/rest/error_handlers.h" |
| 21 | + |
| 22 | +#include <string_view> |
| 23 | + |
| 24 | +namespace iceberg::rest { |
| 25 | + |
| 26 | +namespace { |
| 27 | + |
| 28 | +constexpr std::string_view kIllegalArgumentException = "IllegalArgumentException"; |
| 29 | +constexpr std::string_view kNoSuchNamespaceException = "NoSuchNamespaceException"; |
| 30 | +constexpr std::string_view kNamespaceNotEmptyException = "NamespaceNotEmptyException"; |
| 31 | + |
| 32 | +} // namespace |
| 33 | + |
| 34 | +const std::shared_ptr<DefaultErrorHandler>& DefaultErrorHandler::Instance() { |
| 35 | + static const std::shared_ptr<DefaultErrorHandler> instance{new DefaultErrorHandler()}; |
| 36 | + return instance; |
| 37 | +} |
| 38 | + |
| 39 | +Status DefaultErrorHandler::Accept(const ErrorModel& error) const { |
| 40 | + switch (error.code) { |
| 41 | + case 400: |
| 42 | + if (error.type == kIllegalArgumentException) { |
| 43 | + return InvalidArgument(error.message); |
| 44 | + } |
| 45 | + return BadRequest("Malformed request: {}", error.message); |
| 46 | + case 401: |
| 47 | + return NotAuthorized("Not authorized: {}", error.message); |
| 48 | + case 403: |
| 49 | + return Forbidden("Forbidden: {}", error.message); |
| 50 | + case 405: |
| 51 | + case 406: |
| 52 | + break; |
| 53 | + case 500: |
| 54 | + return InternalServerError("Server error: {}: {}", error.type, error.message); |
| 55 | + case 501: |
| 56 | + return NotSupported(error.message); |
| 57 | + case 503: |
| 58 | + return ServiceUnavailable("Service unavailable: {}", error.message); |
| 59 | + } |
| 60 | + |
| 61 | + return RestError("Unable to process: {}", error.message); |
| 62 | +} |
| 63 | + |
| 64 | +const std::shared_ptr<NamespaceErrorHandler>& NamespaceErrorHandler::Instance() { |
| 65 | + static const std::shared_ptr<NamespaceErrorHandler> instance{ |
| 66 | + new NamespaceErrorHandler()}; |
| 67 | + return instance; |
| 68 | +} |
| 69 | + |
| 70 | +Status NamespaceErrorHandler::Accept(const ErrorModel& error) const { |
| 71 | + switch (error.code) { |
| 72 | + case 400: |
| 73 | + if (error.type == kNamespaceNotEmptyException) { |
| 74 | + return NamespaceNotEmpty(error.message); |
| 75 | + } |
| 76 | + return BadRequest("Malformed request: {}", error.message); |
| 77 | + case 404: |
| 78 | + return NoSuchNamespace(error.message); |
| 79 | + case 409: |
| 80 | + return AlreadyExists(error.message); |
| 81 | + case 422: |
| 82 | + return RestError("Unable to process: {}", error.message); |
| 83 | + } |
| 84 | + |
| 85 | + return DefaultErrorHandler::Accept(error); |
| 86 | +} |
| 87 | + |
| 88 | +const std::shared_ptr<DropNamespaceErrorHandler>& DropNamespaceErrorHandler::Instance() { |
| 89 | + static const std::shared_ptr<DropNamespaceErrorHandler> instance{ |
| 90 | + new DropNamespaceErrorHandler()}; |
| 91 | + return instance; |
| 92 | +} |
| 93 | + |
| 94 | +Status DropNamespaceErrorHandler::Accept(const ErrorModel& error) const { |
| 95 | + if (error.code == 409) { |
| 96 | + return NamespaceNotEmpty(error.message); |
| 97 | + } |
| 98 | + |
| 99 | + return NamespaceErrorHandler::Accept(error); |
| 100 | +} |
| 101 | + |
| 102 | +const std::shared_ptr<TableErrorHandler>& TableErrorHandler::Instance() { |
| 103 | + static const std::shared_ptr<TableErrorHandler> instance{new TableErrorHandler()}; |
| 104 | + return instance; |
| 105 | +} |
| 106 | + |
| 107 | +Status TableErrorHandler::Accept(const ErrorModel& error) const { |
| 108 | + switch (error.code) { |
| 109 | + case 404: |
| 110 | + if (error.type == kNoSuchNamespaceException) { |
| 111 | + return NoSuchNamespace(error.message); |
| 112 | + } |
| 113 | + return NoSuchTable(error.message); |
| 114 | + case 409: |
| 115 | + return AlreadyExists(error.message); |
| 116 | + } |
| 117 | + |
| 118 | + return DefaultErrorHandler::Accept(error); |
| 119 | +} |
| 120 | + |
| 121 | +const std::shared_ptr<ViewErrorHandler>& ViewErrorHandler::Instance() { |
| 122 | + static const std::shared_ptr<ViewErrorHandler> instance{new ViewErrorHandler()}; |
| 123 | + return instance; |
| 124 | +} |
| 125 | + |
| 126 | +Status ViewErrorHandler::Accept(const ErrorModel& error) const { |
| 127 | + switch (error.code) { |
| 128 | + case 404: |
| 129 | + if (error.type == kNoSuchNamespaceException) { |
| 130 | + return NoSuchNamespace(error.message); |
| 131 | + } |
| 132 | + return NoSuchView(error.message); |
| 133 | + case 409: |
| 134 | + return AlreadyExists(error.message); |
| 135 | + } |
| 136 | + |
| 137 | + return DefaultErrorHandler::Accept(error); |
| 138 | +} |
| 139 | + |
| 140 | +const std::shared_ptr<TableCommitErrorHandler>& TableCommitErrorHandler::Instance() { |
| 141 | + static const std::shared_ptr<TableCommitErrorHandler> instance{ |
| 142 | + new TableCommitErrorHandler()}; |
| 143 | + return instance; |
| 144 | +} |
| 145 | + |
| 146 | +Status TableCommitErrorHandler::Accept(const ErrorModel& error) const { |
| 147 | + switch (error.code) { |
| 148 | + case 404: |
| 149 | + return NoSuchTable(error.message); |
| 150 | + case 409: |
| 151 | + return CommitFailed("Commit failed: {}", error.message); |
| 152 | + case 500: |
| 153 | + case 502: |
| 154 | + case 503: |
| 155 | + case 504: |
| 156 | + return CommitStateUnknown("Service failed: {}: {}", error.code, error.message); |
| 157 | + } |
| 158 | + |
| 159 | + return DefaultErrorHandler::Accept(error); |
| 160 | +} |
| 161 | + |
| 162 | +const std::shared_ptr<ViewCommitErrorHandler>& ViewCommitErrorHandler::Instance() { |
| 163 | + static const std::shared_ptr<ViewCommitErrorHandler> instance{ |
| 164 | + new ViewCommitErrorHandler()}; |
| 165 | + return instance; |
| 166 | +} |
| 167 | + |
| 168 | +Status ViewCommitErrorHandler::Accept(const ErrorModel& error) const { |
| 169 | + switch (error.code) { |
| 170 | + case 404: |
| 171 | + return NoSuchView(error.message); |
| 172 | + case 409: |
| 173 | + return CommitFailed("Commit failed: {}", error.message); |
| 174 | + case 500: |
| 175 | + case 502: |
| 176 | + case 503: |
| 177 | + case 504: |
| 178 | + return CommitStateUnknown("Service failed: {}: {}", error.code, error.message); |
| 179 | + } |
| 180 | + |
| 181 | + return DefaultErrorHandler::Accept(error); |
| 182 | +} |
| 183 | + |
| 184 | +} // namespace iceberg::rest |
0 commit comments