alien-bindings 1.4.0

Alien platform runtime bindings
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
use alien_error::{AlienError, AlienErrorData, ContextError};
use serde::{Deserialize, Serialize};

/// Errors related to alien-bindings operations.
#[derive(Debug, Clone, AlienErrorData, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum ErrorData {
    /// Binding provider configuration is invalid or missing.
    #[error(
        code = "BINDING_CONFIG_INVALID",
        message = "Binding configuration invalid for binding '{binding_name}': {reason}",
        retryable = "false",
        internal = "false",
        http_status_code = 400
    )]
    BindingConfigInvalid {
        /// Name of the binding
        binding_name: String,
        /// Specific reason why the configuration is invalid
        reason: String,
    },

    /// Storage operation failed due to provider issues.
    #[error(
        code = "STORAGE_OPERATION_FAILED",
        message = "Storage operation failed for binding '{binding_name}': {operation}",
        retryable = "true",
        internal = "false",
        http_status_code = 502
    )]
    StorageOperationFailed {
        /// Name of the storage binding
        binding_name: String,
        /// Description of the operation that failed
        operation: String,
    },

    /// Build operation failed due to provider issues.
    #[error(
        code = "BUILD_OPERATION_FAILED",
        message = "Build operation failed for binding '{binding_name}': {operation}",
        retryable = "true",
        internal = "false",
        http_status_code = 502
    )]
    BuildOperationFailed {
        /// Name of the build binding
        binding_name: String,
        /// Description of the operation that failed
        operation: String,
    },

    /// Required environment variable is missing or invalid.
    #[error(
        code = "ENVIRONMENT_VARIABLE_MISSING",
        message = "Required environment variable '{variable_name}' is missing",
        retryable = "false",
        internal = "false",
        http_status_code = 500
    )]
    EnvironmentVariableMissing {
        /// Name of the missing environment variable
        variable_name: String,
    },

    /// Environment variable has an invalid value.
    #[error(
        code = "INVALID_ENVIRONMENT_VARIABLE",
        message = "Environment variable '{variable_name}' has invalid value '{value}': {reason}",
        retryable = "false",
        internal = "false",
        http_status_code = 500
    )]
    InvalidEnvironmentVariable {
        /// Name of the environment variable
        variable_name: String,
        /// The invalid value
        value: String,
        /// Reason why the value is invalid
        reason: String,
    },

    /// Configuration URL is malformed or invalid.
    #[error(
        code = "INVALID_CONFIGURATION_URL",
        message = "Invalid configuration URL '{url}': {reason}",
        retryable = "false",
        internal = "false",
        http_status_code = 400
    )]
    InvalidConfigurationUrl {
        /// The invalid URL
        url: String,
        /// Specific reason why the URL is invalid
        reason: String,
    },

    /// Event processing failed due to malformed or unsupported data.
    #[error(
        code = "EVENT_PROCESSING_FAILED",
        message = "Event processing failed for type '{event_type}': {reason}",
        retryable = "true",
        internal = "false",
        http_status_code = 400
    )]
    EventProcessingFailed {
        /// Type of event that failed to process
        event_type: String,
        /// Specific reason for the processing failure
        reason: String,
    },

    /// gRPC connection failed or became unavailable.
    #[error(
        code = "GRPC_CONNECTION_FAILED",
        message = "gRPC connection failed to endpoint '{endpoint}': {reason}",
        retryable = "true",
        internal = "false",
        http_status_code = 502
    )]
    GrpcConnectionFailed {
        /// The gRPC endpoint that failed to connect
        endpoint: String,
        /// Reason for the connection failure
        reason: String,
    },

    /// gRPC service unavailable or returned error.
    #[error(
        code = "GRPC_SERVICE_UNAVAILABLE",
        message = "gRPC service '{service}' unavailable at endpoint '{endpoint}': {reason}",
        retryable = "true",
        internal = "false",
        http_status_code = 503
    )]
    GrpcServiceUnavailable {
        /// Name of the gRPC service
        service: String,
        /// The gRPC endpoint
        endpoint: String,
        /// Reason for service unavailability
        reason: String,
    },

    /// gRPC request failed with an error status.
    #[error(
        code = "GRPC_REQUEST_FAILED",
        message = "gRPC request to service '{service}' method '{method}' failed: {details}",
        retryable = "true",
        internal = "false",
        http_status_code = 502
    )]
    GrpcRequestFailed {
        /// Name of the gRPC service
        service: String,
        /// Name of the gRPC method
        method: String,
        /// Error details from the gRPC status
        details: String,
    },

    /// Server failed to bind to the specified address.
    #[error(
        code = "SERVER_BIND_FAILED",
        message = "Failed to bind server to address '{address}': {reason}",
        retryable = "true",
        internal = "true",
        http_status_code = 500
    )]
    ServerBindFailed {
        /// The address that failed to bind
        address: String,
        /// Reason for the bind failure
        reason: String,
    },

    /// Authentication failed for the configured provider.
    #[error(
        code = "AUTHENTICATION_FAILED",
        message = "Authentication failed for provider '{provider}' and binding '{binding_name}': {reason}",
        retryable = "true",
        internal = "false",
        http_status_code = 401
    )]
    AuthenticationFailed {
        /// Name of the provider (aws, gcp, azure, etc.)
        provider: String,
        /// Name of the binding
        binding_name: String,
        /// Reason for authentication failure
        reason: String,
    },

    /// Operation not supported by the configured provider.
    #[error(
        code = "OPERATION_NOT_SUPPORTED",
        message = "Operation '{operation}' not supported: {reason}",
        retryable = "false",
        internal = "false",
        http_status_code = 501
    )]
    OperationNotSupported {
        /// Name of the unsupported operation
        operation: String,
        /// Reason why the operation is not supported
        reason: String,
    },

    /// Feature is not enabled in the compiled binary.
    #[error(
        code = "FEATURE_NOT_ENABLED",
        message = "Feature '{feature}' is not enabled in this build",
        retryable = "false",
        internal = "false",
        http_status_code = 501
    )]
    FeatureNotEnabled {
        /// Name of the feature that is not enabled
        feature: String,
    },

    /// gRPC call failed.
    #[error(
        code = "GRPC_CALL_FAILED",
        message = "gRPC call to service '{service}' method '{method}' failed: {reason}",
        retryable = "true",
        internal = "false",
        http_status_code = 502
    )]
    GrpcCallFailed {
        /// Name of the gRPC service
        service: String,
        /// Name of the gRPC method
        method: String,
        /// Reason for the call failure
        reason: String,
    },

    /// Deserialization of data failed.
    #[error(
        code = "DESERIALIZATION_FAILED",
        message = "Failed to deserialize {type_name}: {message}",
        retryable = "false",
        internal = "false",
        http_status_code = 400
    )]
    DeserializationFailed {
        /// Human-readable error message
        message: String,
        /// Name of the type being deserialized
        type_name: String,
    },

    /// Serialization of data failed.
    #[error(
        code = "SERIALIZATION_FAILED",
        message = "Failed to serialize data: {message}",
        retryable = "false",
        internal = "false",
        http_status_code = 500
    )]
    SerializationFailed {
        /// Human-readable error message
        message: String,
    },

    /// Operation is not implemented yet.
    #[error(
        code = "NOT_IMPLEMENTED",
        message = "Operation '{operation}' is not implemented: {reason}",
        retryable = "false",
        internal = "false",
        http_status_code = 501
    )]
    NotImplemented {
        /// Name of the operation that is not implemented
        operation: String,
        /// Reason why the operation is not implemented
        reason: String,
    },

    /// Response format from provider API is unexpected or missing required fields.
    #[error(
        code = "UNEXPECTED_RESPONSE_FORMAT",
        message = "Unexpected response format from '{provider}' for binding '{binding_name}': missing field '{field}'. Response: {response_json}",
        retryable = "false",
        internal = "false",
        http_status_code = 502
    )]
    UnexpectedResponseFormat {
        /// Name of the provider (aws, gcp, azure, etc.)
        provider: String,
        /// Name of the binding
        binding_name: String,
        /// Name of the missing or malformed field
        field: String,
        /// The full response JSON for debugging
        response_json: String,
    },

    /// Cloud platform API error.
    #[error(
        code = "CLOUD_PLATFORM_ERROR",
        message = "Cloud platform error: {message}",
        retryable = "true",
        internal = "false",
        http_status_code = 502
    )]
    CloudPlatformError {
        /// Human-readable description of the error
        message: String,
        /// Optional resource ID that was involved in the error
        resource_id: Option<String>,
    },

    /// Resource not found in the cloud platform.
    #[error(
        code = "RESOURCE_NOT_FOUND",
        message = "Resource '{resource_id}' not found",
        retryable = "false",
        internal = "false",
        http_status_code = 404
    )]
    ResourceNotFound {
        /// ID of the resource that was not found
        resource_id: String,
    },

    /// The requested remote resource does not exist.
    #[error(
        code = "REMOTE_RESOURCE_NOT_FOUND",
        message = "{operation_context}: {resource_type} '{resource_name}' not found",
        retryable = "false",
        internal = "false",
        http_status_code = 404
    )]
    RemoteResourceNotFound {
        /// Context of the operation that failed (e.g., "Failed to get ECR repository details")
        operation_context: String,
        /// Type of the resource that was not found
        resource_type: String,
        /// Name of the resource that was not found
        resource_name: String,
    },

    /// Operation conflicts with current remote resource state.
    #[error(
        code = "REMOTE_RESOURCE_CONFLICT",
        message = "{operation_context}: Conflict with {resource_type} '{resource_name}' - {conflict_reason}",
        retryable = "true",
        internal = "false",
        http_status_code = 409
    )]
    RemoteResourceConflict {
        /// Context of the operation that failed
        operation_context: String,
        /// Type of the resource that has a conflict
        resource_type: String,
        /// Name of the resource that has a conflict
        resource_name: String,
        /// Specific reason for the conflict
        conflict_reason: String,
    },

    /// Access denied due to insufficient permissions.
    #[error(
        code = "REMOTE_ACCESS_DENIED",
        message = "{operation_context}: Access denied to {resource_type} '{resource_name}'",
        retryable = "true",
        internal = "false",
        http_status_code = 403
    )]
    RemoteAccessDenied {
        /// Context of the operation that failed
        operation_context: String,
        /// Type of the resource access was denied to
        resource_type: String,
        /// Name of the resource access was denied to
        resource_name: String,
    },

    /// Request rate limit exceeded.
    #[error(
        code = "RATE_LIMIT_EXCEEDED",
        message = "{operation_context}: Rate limit exceeded - {details}",
        retryable = "true",
        internal = "false",
        http_status_code = 429
    )]
    RateLimitExceeded {
        /// Context of the operation that failed
        operation_context: String,
        /// Additional details about the rate limit
        details: String,
    },

    /// Operation exceeded the allowed timeout.
    #[error(
        code = "TIMEOUT",
        message = "{operation_context}: Operation timed out - {details}",
        retryable = "true",
        internal = "false",
        http_status_code = 408
    )]
    Timeout {
        /// Context of the operation that failed
        operation_context: String,
        /// Additional details about the timeout
        details: String,
    },

    /// Remote service is temporarily unavailable.
    #[error(
        code = "REMOTE_SERVICE_UNAVAILABLE",
        message = "{operation_context}: Service unavailable - {details}",
        retryable = "true",
        internal = "false",
        http_status_code = 503
    )]
    RemoteServiceUnavailable {
        /// Context of the operation that failed
        operation_context: String,
        /// Additional details about the service unavailability
        details: String,
    },

    /// Quota or resource limits have been exceeded.
    #[error(
        code = "QUOTA_EXCEEDED",
        message = "{operation_context}: Quota exceeded - {details}",
        retryable = "true",
        internal = "false",
        http_status_code = 429
    )]
    QuotaExceeded {
        /// Context of the operation that failed
        operation_context: String,
        /// Additional details about the quota violation
        details: String,
    },

    /// Invalid or malformed input parameters provided to the operation.
    #[error(
        code = "INVALID_INPUT",
        message = "{operation_context}: Invalid input - {details}",
        retryable = "false",
        internal = "false",
        http_status_code = 400
    )]
    InvalidInput {
        /// Context of the operation that failed
        operation_context: String,
        /// Details about what input was invalid
        details: String,
        /// Optional field name that was invalid
        field_name: Option<String>,
    },

    /// Authentication with cloud provider failed.
    #[error(
        code = "AUTHENTICATION_ERROR",
        message = "{operation_context}: Authentication failed - {details}",
        retryable = "true",
        internal = "false",
        http_status_code = 401
    )]
    AuthenticationError {
        /// Context of the operation that failed
        operation_context: String,
        /// Details about the authentication failure
        details: String,
    },

    /// Generic bindings error for uncommon cases.
    #[error(
        code = "BINDINGS_ERROR",
        message = "Bindings error: {message}",
        retryable = "true",
        internal = "true",
        http_status_code = 500
    )]
    Other {
        /// Human-readable description of the error
        message: String,
    },

    /// Presigned request has expired and can no longer be used.
    #[error(
        code = "PRESIGNED_REQUEST_EXPIRED",
        message = "Presigned request for path '{path}' expired at {expired_at}",
        retryable = "false",
        internal = "false",
        http_status_code = 403
    )]
    PresignedRequestExpired {
        /// Path that the presigned request was for
        path: String,
        /// When the request expired
        expired_at: chrono::DateTime<chrono::Utc>,
    },

    /// HTTP request to external service failed.
    #[error(
        code = "HTTP_REQUEST_FAILED",
        message = "HTTP {method} request to '{url}' failed",
        retryable = "true",
        internal = "false",
        http_status_code = 502
    )]
    HttpRequestFailed {
        /// URL that was requested
        url: String,
        /// HTTP method that was used
        method: String,
    },

    /// Local filesystem operation failed.
    #[error(
        code = "LOCAL_FILESYSTEM_ERROR",
        message = "Local filesystem operation '{operation}' failed for path '{path}'",
        retryable = "true",
        internal = "false",
        http_status_code = 500
    )]
    LocalFilesystemError {
        /// Path that the operation was performed on
        path: String,
        /// Operation that failed
        operation: String,
    },

    /// Failed to load platform configuration for the provider.
    #[error(
        code = "client_config_LOAD_FAILED",
        message = "Failed to load platform configuration for provider '{provider}'",
        retryable = "false",
        internal = "false",
        http_status_code = 400
    )]
    ClientConfigLoadFailed {
        /// Name of the provider (aws, gcp, azure, etc.)
        provider: String,
    },

    /// Binding setup failed during initialization.
    #[error(
        code = "BINDING_SETUP_FAILED",
        message = "Binding setup failed for type '{binding_type}': {reason}",
        retryable = "false",
        internal = "false",
        http_status_code = 500
    )]
    BindingSetupFailed {
        /// Type of binding being set up
        binding_type: String,
        /// Reason for the setup failure
        reason: String,
    },

    /// KV operation failed.
    #[error(
        code = "KV_OPERATION_FAILED",
        message = "KV operation '{operation}' failed for key '{key}': {reason}",
        retryable = "true",
        internal = "false",
        http_status_code = 502
    )]
    KvOperationFailed {
        /// The KV operation that failed
        operation: String,
        /// The key involved in the operation
        key: String,
        /// Reason for the operation failure
        reason: String,
    },

    /// Queue operation failed.
    #[error(
        code = "QUEUE_OPERATION_FAILED",
        message = "Queue operation '{operation}' failed: {reason}",
        retryable = "true",
        internal = "false",
        http_status_code = 502
    )]
    QueueOperationFailed {
        /// The queue operation that failed
        operation: String,
        /// Reason for the operation failure
        reason: String,
    },

    /// Remote access to deployment resources failed.
    #[error(
        code = "REMOTE_ACCESS_FAILED",
        message = "Remote access failed during operation: {operation}",
        retryable = "true",
        internal = "false",
        http_status_code = 502
    )]
    RemoteAccessFailed {
        /// Description of the operation that failed
        operation: String,
    },

    /// Client configuration is invalid or missing for the platform.
    #[error(
        code = "CLIENT_CONFIG_INVALID",
        message = "Client configuration invalid for platform '{platform}': {message}",
        retryable = "false",
        internal = "false",
        http_status_code = 400
    )]
    ClientConfigInvalid {
        /// The platform that was expected
        platform: alien_core::Platform,
        /// Description of the configuration issue
        message: String,
    },
}

/// Convenient alias with default error type `ErrorData`.
pub type Result<T, E = ErrorData> = alien_error::Result<T, E>;

/// Convenience alias representing a constructed AlienError with our `ErrorData` payload.
pub type Error = AlienError<ErrorData>;

/// Maps an `alien_client_core::Error` to an appropriate `alien_bindings::Error`.
///
/// Important error types (like resource not found, access denied, etc.) are mapped
/// to their corresponding variants in alien-bindings while preserving the operation context.
/// Less important errors are wrapped in `CloudPlatformError`.
///
/// # Arguments
/// * `cloud_error` - The error from cloud client crates
/// * `operation_context` - Description of the operation that failed (e.g., "Failed to get ECR repository details")
/// * `resource_id` - Optional resource ID for fallback error context
///
/// # Example
/// ```rust
/// use alien_bindings::error::map_cloud_client_error;
///
/// async fn example() {
///     // This would be an actual cloud client operation
///     let result = some_cloud_operation().await
///         .map_err(|e| map_cloud_client_error(e, "Failed to get ECR repository details".to_string(), Some("my-repo".to_string())));
/// }
///
/// async fn some_cloud_operation() -> Result<(), alien_client_core::Error> {
///     // Mock implementation
///     Ok(())
/// }
/// ```
pub fn map_cloud_client_error(
    cloud_error: alien_client_core::Error,
    operation_context: String,
    resource_id: Option<String>,
) -> Error {
    use alien_client_core::ErrorData as CloudErrorData;

    // Check the error type first to determine the right context to add
    let error_data = match cloud_error.error.as_ref() {
        Some(CloudErrorData::RemoteResourceNotFound {
            resource_type,
            resource_name,
        }) => ErrorData::RemoteResourceNotFound {
            operation_context,
            resource_type: resource_type.clone(),
            resource_name: resource_name.clone(),
        },
        Some(CloudErrorData::RemoteResourceConflict {
            resource_type,
            resource_name,
            message,
        }) => ErrorData::RemoteResourceConflict {
            operation_context,
            resource_type: resource_type.clone(),
            resource_name: resource_name.clone(),
            conflict_reason: message.clone(),
        },
        Some(CloudErrorData::RemoteAccessDenied {
            resource_type,
            resource_name,
        }) => ErrorData::RemoteAccessDenied {
            operation_context,
            resource_type: resource_type.clone(),
            resource_name: resource_name.clone(),
        },
        Some(CloudErrorData::RateLimitExceeded { message }) => ErrorData::RateLimitExceeded {
            operation_context,
            details: message.clone(),
        },
        Some(CloudErrorData::Timeout { message }) => ErrorData::Timeout {
            operation_context,
            details: message.clone(),
        },
        Some(CloudErrorData::RemoteServiceUnavailable { message }) => {
            ErrorData::RemoteServiceUnavailable {
                operation_context,
                details: message.clone(),
            }
        }
        Some(CloudErrorData::QuotaExceeded { message }) => ErrorData::QuotaExceeded {
            operation_context,
            details: message.clone(),
        },
        Some(CloudErrorData::InvalidInput {
            message,
            field_name,
        }) => ErrorData::InvalidInput {
            operation_context,
            details: message.clone(),
            field_name: field_name.clone(),
        },
        Some(CloudErrorData::AuthenticationError { message }) => ErrorData::AuthenticationError {
            operation_context,
            details: message.clone(),
        },
        // For other error types or None, wrap in CloudPlatformError
        _ => ErrorData::CloudPlatformError {
            message: operation_context,
            resource_id,
        },
    };

    // Now add the context to the cloud error
    cloud_error.context(error_data)
}