StripeKit 0.1.1
Stripe integration toolkit for C++
Loading...
Searching...
No Matches
Errors.h
1/** Errors.h
2 *
3 * Exception and error payload types for StripeKit public APIs.
4 *
5 * @author Hans de Ruiter
6 *
7 * @license See LICENSE.md for details.
8 */
9
10#pragma once
11
12#include <stdexcept>
13#include <string>
14
15namespace stripekit {
16
17/**
18 * @addtogroup errors
19 * @{
20 */
21
22/**
23 * What kind of thing went wrong, as categorised by Stripe.
24 *
25 * @sa https://docs.stripe.com/error-handling
26 */
27enum class StripeErrorType {
28 Api, /**< Something went wrong on Stripe's side. Worth retrying. */
29 Card, /**< The card was declined. Ask the customer for another one. */
30 Idempotency, /**< An idempotency key was reused with different parameters. */
31 InvalidRequest, /**< The request was malformed, or referred to something that does not exist. */
32 Authentication, /**< The API key is missing, wrong, or revoked. */
33 Permission, /**< The key is valid but not allowed to do this. */
34 RateLimit, /**< Too many requests too quickly. Back off and retry. */
35 ApiConnection, /**< Stripe could not be reached. Worth retrying. */
36 Unknown /**< The error could not be categorised. */
37};
38
39/**
40 * The details Stripe returned about a failure.
41 *
42 * Always log #request_id: it identifies the exact call in Stripe's own
43 * dashboard logs, which turns most "why did this fail" questions into a quick
44 * lookup.
45 */
47 /** What kind of thing went wrong. */
49
50 /** Stripe's specific error code, for example "card_declined". */
51 std::string code;
52
53 /** Stripe's human-readable description of the failure. */
54 std::string message;
55
56 /** HTTP status Stripe returned, or 0 if Stripe was never reached. */
57 int http_status = 0;
58
59 /** Id of the failed request in Stripe's logs, of the form "req_...". */
60 std::string request_id;
61
62 /** True when the same request may succeed if tried again later. */
63 bool retryable = false;
64};
65
66/**
67 * Base class for every exception StripeKit throws.
68 *
69 * Catch this to handle any StripeKit failure in one place; catch the derived
70 * types when the distinction matters.
71 */
72class StripeKitException : public std::runtime_error {
73public:
74 /**
75 * Creates an exception carrying a descriptive message.
76 *
77 * @param message Text describing what went wrong.
78 */
79 explicit StripeKitException(const std::string& message);
80
81 /**
82 * Destroys the exception instance.
83 */
85};
86
87/**
88 * Thrown when Stripe rejects a request, or cannot be reached.
89 *
90 * @code
91 * try {
92 * const auto session = co_await stripe->checkout().sessions().create_async(request);
93 * } catch (const stripekit::StripeApiException& error) {
94 * log_error(error.payload().message, error.payload().request_id);
95 * }
96 * @endcode
97 */
99public:
100 /**
101 * Creates an API exception from Stripe's error details.
102 *
103 * @param payload The parsed error details.
104 */
106
107 /**
108 * Returns the details Stripe gave for the failure.
109 *
110 * @return A reference valid for the lifetime of the exception.
111 */
112 const StripeErrorPayload& payload() const noexcept;
113
114 /**
115 * Returns whether trying the same request again may succeed.
116 *
117 * @return True for transient failures such as rate limits and connection
118 * problems; false for anything that will fail the same way again.
119 */
120 bool retryable() const noexcept;
121
122private:
123 StripeErrorPayload payload_data;
124};
125
126/**
127 * Thrown when a webhook delivery cannot be trusted.
128 *
129 * Either the signature does not match the payload, or its timestamp is outside
130 * the allowed tolerance. Answer such a request with HTTP 400: the delivery did
131 * not come from Stripe, or was tampered with, and retrying will not help.
132 */
134public:
135 /**
136 * Creates a signature verification exception.
137 *
138 * @param message Text describing which check failed.
139 */
140 explicit SignatureVerificationException(const std::string& message);
141
142 /**
143 * Destroys the exception instance.
144 */
146};
147
148/**
149 * Thrown when StripeKit is set up in a way it cannot work with.
150 *
151 * Typical causes are a malformed webhook payload, or asking for a feature the
152 * library was not built with. These indicate a problem to fix rather than a
153 * transient failure to retry.
154 */
156public:
157 /**
158 * Creates a configuration exception.
159 *
160 * @param message Text describing what is misconfigured.
161 */
162 explicit ConfigurationException(const std::string& message);
163
164 /**
165 * Destroys the exception instance.
166 */
168};
169
170/** @} */
171
172} // namespace stripekit
ConfigurationException(const std::string &message)
Creates a configuration exception.
~ConfigurationException() override
Destroys the exception instance.
~SignatureVerificationException() override
Destroys the exception instance.
SignatureVerificationException(const std::string &message)
Creates a signature verification exception.
const StripeErrorPayload & payload() const noexcept
Returns the details Stripe gave for the failure.
StripeApiException(StripeErrorPayload payload)
Creates an API exception from Stripe's error details.
bool retryable() const noexcept
Returns whether trying the same request again may succeed.
StripeKitException(const std::string &message)
Creates an exception carrying a descriptive message.
~StripeKitException() override
Destroys the exception instance.
StripeErrorType
What kind of thing went wrong, as categorised by Stripe.
Definition Errors.h:27
@ Idempotency
An idempotency key was reused with different parameters.
Definition Errors.h:30
@ Card
The card was declined.
Definition Errors.h:29
@ RateLimit
Too many requests too quickly.
Definition Errors.h:34
@ Api
Something went wrong on Stripe's side.
Definition Errors.h:28
@ Unknown
The error could not be categorised.
Definition Errors.h:36
@ Authentication
The API key is missing, wrong, or revoked.
Definition Errors.h:32
@ InvalidRequest
The request was malformed, or referred to something that does not exist.
Definition Errors.h:31
@ ApiConnection
Stripe could not be reached.
Definition Errors.h:35
@ Permission
The key is valid but not allowed to do this.
Definition Errors.h:33
Async.h.
Definition Async.h:20
The details Stripe returned about a failure.
Definition Errors.h:46
std::string message
Stripe's human-readable description of the failure.
Definition Errors.h:54
std::string code
Stripe's specific error code, for example "card_declined".
Definition Errors.h:51
bool retryable
True when the same request may succeed if tried again later.
Definition Errors.h:63
int http_status
HTTP status Stripe returned, or 0 if Stripe was never reached.
Definition Errors.h:57
StripeErrorType type
What kind of thing went wrong.
Definition Errors.h:48
std::string request_id
Id of the failed request in Stripe's logs, of the form "req_...".
Definition Errors.h:60