force 0.2.0

Production-ready Salesforce Platform API client with REST and Bulk API 2.0 support
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
//! Error types for the Force API client.
//!
//! This module provides a comprehensive error hierarchy for all Salesforce API operations.

use std::fmt;

/// The main error type for Force API operations.
///
/// This error type provides detailed information about failures that can occur
/// when interacting with Salesforce APIs.
#[derive(Debug, thiserror::Error)]
pub enum ForceError {
    /// Authentication-related errors.
    #[error("authentication failed: {0}")]
    Authentication(#[from] AuthenticationError),

    /// HTTP communication errors.
    #[error("HTTP request failed: {0}")]
    Http(#[from] HttpError),

    /// Salesforce API-specific errors.
    #[error("Salesforce API error: {0}")]
    Api(#[from] ApiError),

    /// Configuration errors.
    #[error("configuration error: {0}")]
    Config(#[from] ConfigError),

    /// Serialization/deserialization errors.
    #[error("serialization error: {0}")]
    Serialization(#[from] SerializationError),

    /// Invalid Salesforce ID.
    #[error("invalid Salesforce ID: {0}")]
    InvalidId(#[from] crate::types::salesforce_id::SalesforceIdError),

    /// Invalid input provided to an API method.
    #[error("invalid input: {0}")]
    InvalidInput(String),
    /// I/O error.
    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),

    /// Feature not yet implemented.
    #[error("not implemented: {0}")]
    NotImplemented(String),

    /// GraphQL API errors (feature-gated).
    #[cfg(feature = "graphql")]
    #[error("GraphQL error: {0}")]
    GraphQL(#[from] crate::api::graphql::GraphqlErrorResponse),

    /// CPQ API errors (feature-gated).
    #[cfg(feature = "cpq")]
    #[error("CPQ error: {0}")]
    Cpq(#[from] crate::api::cpq::CpqErrorResponse),
}

/// Authentication-related errors.
#[derive(Debug, thiserror::Error)]
pub enum AuthenticationError {
    /// OAuth token request failed.
    #[error("OAuth token request failed: {0}")]
    TokenRequestFailed(String),

    /// Invalid credentials provided.
    #[error("invalid credentials: {0}")]
    InvalidCredentials(String),

    /// Token has expired.
    #[error("access token has expired")]
    TokenExpired,

    /// Token refresh failed.
    #[error("failed to refresh access token: {0}")]
    TokenRefreshFailed(String),

    /// Invalid token state (internal error).
    #[error("invalid token state")]
    InvalidToken,

    /// JWT token creation failed (feature-gated).
    #[cfg(feature = "jwt")]
    #[error("JWT token creation failed: {0}")]
    JwtCreationFailed(String),

    /// Invalid JWT configuration (feature-gated).
    #[cfg(feature = "jwt")]
    #[error("invalid JWT configuration: {0}")]
    InvalidJwtConfig(String),
}

/// HTTP communication errors.
#[derive(Debug, thiserror::Error)]
pub enum HttpError {
    /// Network request failed.
    #[error("network request failed: {0}")]
    RequestFailed(#[from] reqwest::Error),

    /// HTTP status code indicates an error.
    #[error("HTTP {status_code}: {message}")]
    StatusError {
        /// The HTTP status code.
        status_code: u16,
        /// Error message.
        message: String,
    },

    /// Rate limit exceeded.
    #[error("rate limit exceeded, retry after {retry_after_seconds} seconds")]
    RateLimitExceeded {
        /// Number of seconds to wait before retrying.
        retry_after_seconds: u64,
    },

    /// Request timeout.
    #[error("request timeout after {timeout_seconds} seconds")]
    Timeout {
        /// Timeout duration in seconds.
        timeout_seconds: u64,
    },

    /// Invalid URL.
    #[error("invalid URL: {0}")]
    InvalidUrl(String),

    /// Response payload too large.
    #[error("response payload exceeded the safety limit of {limit_bytes} bytes")]
    PayloadTooLarge {
        /// The byte limit that was exceeded.
        limit_bytes: usize,
    },
}

/// Salesforce API-specific errors.
///
/// Error information from a failed Salesforce API operation.
///
/// When an operation fails, Salesforce returns detailed error information
/// including a message, error code, and the fields that caused the error.
///
/// # Examples
///
/// ```
/// use force::error::ApiError;
///
/// let error = ApiError {
///     message: "Required fields are missing: [Name]".to_string(),
///     error_code: "REQUIRED_FIELD_MISSING".to_string(),
///     fields: vec!["Name".to_string()],
/// };
/// assert_eq!(error.error_code, "REQUIRED_FIELD_MISSING");
/// ```
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize, thiserror::Error)]
#[serde(rename_all = "camelCase")]
pub struct ApiError {
    /// Human-readable error message.
    pub message: String,

    /// Error code from Salesforce.
    #[serde(alias = "statusCode", alias = "errorCode")]
    pub error_code: String,

    /// Additional error fields from the API response.
    #[serde(default)]
    pub fields: Vec<String>,
}

impl ApiError {
    /// Creates a new API error.
    #[must_use]
    pub fn new(message: impl Into<String>, error_code: impl Into<String>) -> Self {
        Self {
            message: message.into(),
            error_code: error_code.into(),
            fields: Vec::new(),
        }
    }

    /// Creates a new API error with associated fields.
    #[must_use]
    pub fn with_fields(
        message: impl Into<String>,
        error_code: impl Into<String>,
        fields: Vec<String>,
    ) -> Self {
        Self {
            message: message.into(),
            error_code: error_code.into(),
            fields,
        }
    }
}

impl fmt::Display for ApiError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "[{}] {}", self.error_code, self.message)?;
        if !self.fields.is_empty() {
            write!(f, " (fields: ")?;
            for (i, field) in self.fields.iter().enumerate() {
                if i > 0 {
                    write!(f, ", ")?;
                }
                write!(f, "{}", field)?;
            }
            write!(f, ")")?;
        }
        Ok(())
    }
}

/// Configuration errors.
#[derive(Debug, thiserror::Error)]
pub enum ConfigError {
    /// Missing required configuration value.
    #[error("missing required configuration: {0}")]
    MissingValue(String),

    /// Invalid configuration value.
    #[error("invalid configuration value for {field}: {reason}")]
    InvalidValue {
        /// The configuration field name.
        field: String,
        /// Reason why the value is invalid.
        reason: String,
    },

    /// Environment variable error.
    #[error("environment variable error: {0}")]
    EnvVar(#[from] std::env::VarError),
}

/// Serialization/deserialization errors.
#[derive(Debug, thiserror::Error)]
pub enum SerializationError {
    /// JSON serialization/deserialization error.
    #[error("JSON error: {0}")]
    Json(#[from] serde_json::Error),

    /// CSV serialization/deserialization error (feature-gated).
    #[cfg(feature = "bulk")]
    #[error("CSV error: {0}")]
    Csv(#[from] csv::Error),

    /// Invalid data format.
    #[error("invalid data format: {0}")]
    InvalidFormat(String),
}

/// A specialized Result type for Force API operations.
pub type Result<T> = std::result::Result<T, ForceError>;
#[cfg(test)]
mod tests {
    use super::*;
    use crate::test_support::Must;

    #[test]
    fn test_authentication_error_display() {
        let err = AuthenticationError::TokenRequestFailed("invalid_grant".to_string());
        assert_eq!(err.to_string(), "OAuth token request failed: invalid_grant");
    }

    #[test]
    fn test_authentication_error_token_expired() {
        let err = AuthenticationError::TokenExpired;
        assert_eq!(err.to_string(), "access token has expired");
    }

    #[test]
    fn test_http_error_status() {
        let err = HttpError::StatusError {
            status_code: 404,
            message: "Resource not found".to_string(),
        };
        assert_eq!(err.to_string(), "HTTP 404: Resource not found");
    }

    #[test]
    fn test_http_error_rate_limit() {
        let err = HttpError::RateLimitExceeded {
            retry_after_seconds: 60,
        };
        assert_eq!(
            err.to_string(),
            "rate limit exceeded, retry after 60 seconds"
        );
    }

    #[test]
    fn test_http_error_timeout() {
        let err = HttpError::Timeout {
            timeout_seconds: 30,
        };
        assert_eq!(err.to_string(), "request timeout after 30 seconds");
    }

    #[test]
    fn test_api_error_display() {
        let err = ApiError {
            error_code: "INVALID_FIELD".to_string(),
            message: "Field does not exist".to_string(),
            fields: vec!["Account.InvalidField".to_string()],
        };
        assert_eq!(
            err.to_string(),
            "[INVALID_FIELD] Field does not exist (fields: Account.InvalidField)"
        );
    }

    #[test]
    fn test_api_error_no_fields() {
        let err = ApiError {
            error_code: "UNKNOWN_ERROR".to_string(),
            message: "An unknown error occurred".to_string(),
            fields: vec![],
        };
        assert_eq!(err.to_string(), "[UNKNOWN_ERROR] An unknown error occurred");
    }

    #[test]
    fn test_config_error_missing_value() {
        let err = ConfigError::MissingValue("client_id".to_string());
        assert_eq!(err.to_string(), "missing required configuration: client_id");
    }

    #[test]
    fn test_config_error_invalid_value() {
        let err = ConfigError::InvalidValue {
            field: "api_version".to_string(),
            reason: "must be in format vXX.0".to_string(),
        };
        assert_eq!(
            err.to_string(),
            "invalid configuration value for api_version: must be in format vXX.0"
        );
    }

    #[test]
    fn test_serialization_error_invalid_format() {
        let err = SerializationError::InvalidFormat("expected ISO 8601 date".to_string());
        assert_eq!(
            err.to_string(),
            "invalid data format: expected ISO 8601 date"
        );
    }

    #[test]
    fn test_force_error_from_authentication() {
        let auth_err = AuthenticationError::TokenExpired;
        let force_err: ForceError = auth_err.into();
        assert_eq!(
            force_err.to_string(),
            "authentication failed: access token has expired"
        );
    }

    #[test]
    fn test_force_error_from_http() {
        let http_err = HttpError::InvalidUrl("not a valid url".to_string());
        let force_err: ForceError = http_err.into();
        assert_eq!(
            force_err.to_string(),
            "HTTP request failed: invalid URL: not a valid url"
        );
    }

    #[test]
    fn test_force_error_from_api() {
        let api_err = ApiError {
            error_code: "REQUIRED_FIELD_MISSING".to_string(),
            message: "Name is required".to_string(),
            fields: vec!["Name".to_string()],
        };
        let force_err: ForceError = api_err.into();
        assert_eq!(
            force_err.to_string(),
            "Salesforce API error: [REQUIRED_FIELD_MISSING] Name is required (fields: Name)"
        );
    }

    #[test]
    fn test_force_error_from_config() {
        let config_err = ConfigError::MissingValue("instance_url".to_string());
        let force_err: ForceError = config_err.into();
        assert_eq!(
            force_err.to_string(),
            "configuration error: missing required configuration: instance_url"
        );
    }

    #[test]
    fn test_force_error_from_serialization() {
        let ser_err = SerializationError::InvalidFormat("malformed JSON".to_string());
        let force_err: ForceError = ser_err.into();
        assert_eq!(
            force_err.to_string(),
            "serialization error: invalid data format: malformed JSON"
        );
    }

    #[test]
    fn test_result_type_ok() {
        let result: Result<i32> = Ok(42);
        assert!(result.is_ok());
        assert_eq!(result.must(), 42);
    }

    #[test]
    fn test_result_type_err() {
        let result: Result<i32> = Err(ForceError::Authentication(
            AuthenticationError::TokenExpired,
        ));
        let Err(err) = result else {
            panic!("Expected an error");
        };
        assert!(err.to_string().contains(""));
    }

    #[cfg(feature = "jwt")]
    #[test]
    fn test_jwt_error_creation_failed() {
        let err = AuthenticationError::JwtCreationFailed("invalid key".to_string());
        assert_eq!(err.to_string(), "JWT token creation failed: invalid key");
    }

    #[cfg(feature = "jwt")]
    #[test]
    fn test_jwt_error_invalid_config() {
        let err = AuthenticationError::InvalidJwtConfig("missing private key".to_string());
        assert_eq!(
            err.to_string(),
            "invalid JWT configuration: missing private key"
        );
    }

    #[cfg(feature = "bulk")]
    #[test]
    fn test_csv_error_conversion() {
        // CSV errors require actual CSV parsing to generate, so we test the variant exists
        let err = SerializationError::InvalidFormat("CSV test".to_string());
        assert!(err.to_string().contains("invalid data format"));
    }

    #[test]
    fn test_force_error_from_invalid_id_length() {
        let id_err = crate::types::salesforce_id::SalesforceIdError::InvalidLength(10);
        let force_err: ForceError = id_err.into();
        assert_eq!(
            force_err.to_string(),
            "invalid Salesforce ID: invalid ID length: 10 (must be 15 or 18 characters)"
        );
    }

    #[test]
    fn test_force_error_from_invalid_id_characters() {
        let id_err = crate::types::salesforce_id::SalesforceIdError::InvalidCharacters;
        let force_err: ForceError = id_err.into();
        assert_eq!(
            force_err.to_string(),
            "invalid Salesforce ID: ID contains invalid characters (must be alphanumeric)"
        );
    }

    #[test]
    fn test_force_error_from_invalid_id_checksum() {
        let id_err = crate::types::salesforce_id::SalesforceIdError::InvalidChecksum;
        let force_err: ForceError = id_err.into();
        assert_eq!(
            force_err.to_string(),
            "invalid Salesforce ID: invalid checksum for 18-character ID"
        );
    }

    #[test]
    fn test_error_trait_implementations() {
        // Verify all errors implement Send + Sync + 'static
        fn assert_send_sync<T: Send + Sync + 'static>() {}

        assert_send_sync::<ForceError>();
        assert_send_sync::<AuthenticationError>();
        assert_send_sync::<HttpError>();
        assert_send_sync::<ApiError>();
        assert_send_sync::<ConfigError>();
        assert_send_sync::<SerializationError>();
    }
}