olp-cpp-sdk  1.22.0
ApiError.h
1 /*
2  * Copyright (C) 2019-2025 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 CacheIO(const char* description = "Cache IO") {
109  return {ErrorCode::CacheIO, description};
110  }
111 
120  static ApiError Offline(const char* description = "Offline") {
121  return {ErrorCode::Offline, description};
122  }
123 
132  static ApiError Unknown(const char* message = "Unknown") {
133  return {ErrorCode::Unknown, message};
134  }
135 
136  ApiError() = default;
137 
150  ApiError(ErrorCode error_code, std::string message, bool is_retryable = false)
151  : error_code_(error_code),
152  message_(std::move(message)),
153  is_retryable_(is_retryable) {
154  if (error_code == ErrorCode::Cancelled) {
155  http_status_code_ =
156  static_cast<int>(olp::http::ErrorCode::CANCELLED_ERROR);
157  }
158  }
159 
170  ApiError(int http_status_code, std::string message = "") // NOLINT
171  : error_code_(http::HttpStatusCode::GetErrorCode(http_status_code)),
172  http_status_code_(http_status_code),
173  message_(std::move(message)),
174  is_retryable_(http::HttpStatusCode::IsRetryable(http_status_code)) {}
175 
181  inline ErrorCode GetErrorCode() const { return error_code_; }
182 
188  inline int GetHttpStatusCode() const { return http_status_code_; }
189 
195  inline const std::string& GetMessage() const { return message_; }
196 
202  inline bool ShouldRetry() const { return is_retryable_; }
203 
204  private:
205  // TODO: merge error_code_ and http_status_code_ by shifting ErrorCode values
206  // to negatives
207  ErrorCode error_code_{ErrorCode::Unknown};
208  int http_status_code_{static_cast<int>(http::ErrorCode::UNKNOWN_ERROR)};
209  std::string message_;
210  bool is_retryable_{false};
211 };
212 
213 } // namespace client
214 } // 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:181
static ApiError Offline(const char *description="Offline")
Creates the ApiError instance with the offline error code and description.
Definition: ApiError.h:120
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:202
const std::string & GetMessage() const
Gets the error message.
Definition: ApiError.h:195
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:188
static ApiError PreconditionFailed(const char *message="Precondition failed")
Creates the ApiError instance with the precondition failed error code and description.
Definition: ApiError.h:71
static ApiError CacheIO(const char *description="Cache IO")
Creates the ApiError instance with the cache IO error code and description.
Definition: ApiError.h:108
ApiError(ErrorCode error_code, std::string message, bool is_retryable=false)
Creates the ApiError instance with the internal error.
Definition: ApiError.h:150
ApiError(int http_status_code, std::string message="")
Creates the ApiError instance with the HTTP status code.
Definition: ApiError.h:170
static ApiError Unknown(const char *message="Unknown")
Creates the ApiError instance with the unknown error code and description.
Definition: ApiError.h:132
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