olp-cpp-sdk  1.19.0
ApiError.h
1 /*
2  * Copyright (C) 2019-2022 HERE Europe B.V.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * SPDX-License-Identifier: Apache-2.0
17  * License-Filename: LICENSE
18  */
19 
20 #pragma once
21 
22 #include <string>
23 #include <utility>
24 
25 #include "ErrorCode.h"
26 
27 #include <olp/core/CoreApi.h>
28 #include "olp/core/client/HttpResponse.h"
29 #include "olp/core/http/HttpStatusCode.h"
30 
31 namespace olp {
32 namespace client {
33 
37 class CORE_API ApiError {
38  public:
47  static ApiError Cancelled(const char* message = "Cancelled") {
48  return {ErrorCode::Cancelled, message};
49  }
50 
59  static ApiError NetworkConnection(const char* message = "Offline") {
60  return {ErrorCode::NetworkConnection, message};
61  }
62 
72  const char* message = "Precondition failed") {
73  return {ErrorCode::PreconditionFailed, message};
74  }
75 
84  static ApiError InvalidArgument(const char* message = "Invalid argument") {
85  return {ErrorCode::InvalidArgument, message};
86  }
87 
96  static ApiError NotFound(const char* message = "Resource not found") {
97  return {ErrorCode::NotFound, message};
98  }
99 
108  static ApiError Unknown(const char* message = "Unknown") {
109  return {ErrorCode::Unknown, message};
110  }
111 
112  ApiError() = default;
113 
126  ApiError(ErrorCode error_code, std::string message, bool is_retryable = false)
127  : error_code_(error_code),
128  message_(std::move(message)),
129  is_retryable_(is_retryable) {
130  if (error_code == ErrorCode::Cancelled) {
131  http_status_code_ =
132  static_cast<int>(olp::http::ErrorCode::CANCELLED_ERROR);
133  }
134  }
135 
146  ApiError(int http_status_code, std::string message = "") // NOLINT
147  : error_code_(http::HttpStatusCode::GetErrorCode(http_status_code)),
148  http_status_code_(http_status_code),
149  message_(std::move(message)),
150  is_retryable_(http::HttpStatusCode::IsRetryable(http_status_code)) {}
151 
157  inline ErrorCode GetErrorCode() const { return error_code_; }
158 
164  inline int GetHttpStatusCode() const { return http_status_code_; }
165 
171  inline const std::string& GetMessage() const { return message_; }
172 
178  inline bool ShouldRetry() const { return is_retryable_; }
179 
180  private:
181  // TODO: merge error_code_ and http_status_code_ by shifting ErrorCode values
182  // to negatives
183  ErrorCode error_code_{ErrorCode::Unknown};
184  int http_status_code_{static_cast<int>(http::ErrorCode::UNKNOWN_ERROR)};
185  std::string message_;
186  bool is_retryable_{false};
187 };
188 
189 } // namespace client
190 } // namespace olp
A wrapper around an internal error or HTTP status code.
Definition: ApiError.h:37
static ApiError Cancelled(const char *message="Cancelled")
Creates the ApiError instance with the cancelled error code and description.
Definition: ApiError.h:47
ErrorCode GetErrorCode() const
Gets the error code.
Definition: ApiError.h:157
static ApiError NetworkConnection(const char *message="Offline")
Creates the ApiError instance with the network connection error code and description.
Definition: ApiError.h:59
bool ShouldRetry() const
Checks if the request can be retried for this error.
Definition: ApiError.h:178
const std::string & GetMessage() const
Gets the error message.
Definition: ApiError.h:171
static ApiError InvalidArgument(const char *message="Invalid argument")
Creates the ApiError instance with the invalid argument error code and description.
Definition: ApiError.h:84
int GetHttpStatusCode() const
Gets the HTTP status code.
Definition: ApiError.h:164
static ApiError PreconditionFailed(const char *message="Precondition failed")
Creates the ApiError instance with the precondition failed error code and description.
Definition: ApiError.h:71
ApiError(ErrorCode error_code, std::string message, bool is_retryable=false)
Creates the ApiError instance with the internal error.
Definition: ApiError.h:126
ApiError(int http_status_code, std::string message="")
Creates the ApiError instance with the HTTP status code.
Definition: ApiError.h:146
static ApiError Unknown(const char *message="Unknown")
Creates the ApiError instance with the unknown error code and description.
Definition: ApiError.h:108
static ApiError NotFound(const char *message="Resource not found")
Creates the ApiError instance with the not found error code and description.
Definition: ApiError.h:96
ErrorCode
Represents all possible errors that might happen during a user request.
Definition: ErrorCode.h:29
@ UNKNOWN_ERROR
Internal error that can't be interpreted.
Rules all the other namespaces.
Definition: AppleSignInProperties.h:24