-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.rs
More file actions
2354 lines (2056 loc) · 64.7 KB
/
error.rs
File metadata and controls
2354 lines (2056 loc) · 64.7 KB
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
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! Error types for the windows-erg crate.
use std::borrow::Cow;
use std::fmt;
/// Result type for windows-erg operations.
pub type Result<T> = std::result::Result<T, Error>;
/// Main error type for windows-erg.
#[derive(Debug)]
pub enum Error {
/// A Windows API error occurred.
WindowsApi(WindowsApiError),
/// Access was denied (insufficient permissions).
AccessDenied(AccessDeniedError),
/// The requested resource was not found.
NotFound(NotFoundError),
/// An invalid parameter was provided.
InvalidParameter(InvalidParameterError),
/// A registry-specific error.
Registry(RegistryError),
/// A process-specific error.
Process(ProcessError),
/// A service-specific error.
Service(ServiceError),
/// A thread-specific error.
Thread(ThreadError),
/// An event log-specific error.
EventLog(EventLogError),
/// An ETW-specific error.
Etw(EtwError),
/// A mitigation-specific error.
Mitigation(MitigationError),
/// A proxy-specific error.
Proxy(ProxyError),
/// A security/permissions-specific error.
Security(SecurityError),
/// A file operation error.
FileOperation(FileOperationError),
/// A pipe operation error.
Pipe(PipeError),
/// A desktop/windowing operation error.
Desktop(DesktopError),
/// A generic error with a message.
Other(OtherError),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::WindowsApi(e) => write!(f, "{}", e),
Error::AccessDenied(e) => write!(f, "{}", e),
Error::NotFound(e) => write!(f, "{}", e),
Error::InvalidParameter(e) => write!(f, "{}", e),
Error::Registry(e) => write!(f, "{}", e),
Error::Process(e) => write!(f, "{}", e),
Error::Service(e) => write!(f, "{}", e),
Error::Thread(e) => write!(f, "{}", e),
Error::EventLog(e) => write!(f, "{}", e),
Error::Etw(e) => write!(f, "{}", e),
Error::Mitigation(e) => write!(f, "{}", e),
Error::Proxy(e) => write!(f, "{}", e),
Error::Security(e) => write!(f, "{}", e),
Error::FileOperation(e) => write!(f, "{}", e),
Error::Pipe(e) => write!(f, "{}", e),
Error::Desktop(e) => write!(f, "{}", e),
Error::Other(e) => write!(f, "{}", e),
}
}
}
impl std::error::Error for Error {}
impl From<windows::core::Error> for Error {
fn from(err: windows::core::Error) -> Self {
Error::WindowsApi(WindowsApiError {
inner: err,
context: None,
})
}
}
// ============================================================================
// Structured Error Types
// ============================================================================
/// Windows API error with optional context.
#[derive(Debug)]
pub struct WindowsApiError {
/// The underlying Windows error.
pub inner: windows::core::Error,
/// Optional context about what operation failed.
pub context: Option<Cow<'static, str>>,
}
impl WindowsApiError {
/// Create a new Windows API error.
pub fn new(inner: windows::core::Error) -> Self {
WindowsApiError {
inner,
context: None,
}
}
/// Create a Windows API error with context.
pub fn with_context(
inner: windows::core::Error,
context: impl Into<Cow<'static, str>>,
) -> Self {
WindowsApiError {
inner,
context: Some(context.into()),
}
}
/// Get the Windows error code.
pub fn code(&self) -> i32 {
self.inner.code().0
}
}
impl fmt::Display for WindowsApiError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(context) = &self.context {
write!(f, "Windows API error in {}: {}", context, self.inner)
} else {
write!(f, "Windows API error: {}", self.inner)
}
}
}
impl std::error::Error for WindowsApiError {}
/// Access denied error.
#[derive(Debug)]
pub struct AccessDeniedError {
/// What resource was being accessed.
pub resource: Cow<'static, str>,
/// What operation was attempted.
pub operation: Cow<'static, str>,
/// Optional additional context.
pub reason: Option<Cow<'static, str>>,
}
impl AccessDeniedError {
/// Create a new access denied error.
pub fn new(
resource: impl Into<Cow<'static, str>>,
operation: impl Into<Cow<'static, str>>,
) -> Self {
AccessDeniedError {
resource: resource.into(),
operation: operation.into(),
reason: None,
}
}
/// Create an access denied error with a reason.
pub fn with_reason(
resource: impl Into<Cow<'static, str>>,
operation: impl Into<Cow<'static, str>>,
reason: impl Into<Cow<'static, str>>,
) -> Self {
AccessDeniedError {
resource: resource.into(),
operation: operation.into(),
reason: Some(reason.into()),
}
}
}
impl fmt::Display for AccessDeniedError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(reason) = &self.reason {
write!(
f,
"Access denied: cannot {} '{}' ({})",
self.operation, self.resource, reason
)
} else {
write!(
f,
"Access denied: cannot {} '{}'",
self.operation, self.resource
)
}
}
}
impl std::error::Error for AccessDeniedError {}
/// Resource not found error.
#[derive(Debug)]
pub struct NotFoundError {
/// Type of resource that was not found.
pub resource_type: Cow<'static, str>,
/// Identifier of the resource.
pub identifier: Cow<'static, str>,
}
impl NotFoundError {
/// Create a new not found error.
pub fn new(
resource_type: impl Into<Cow<'static, str>>,
identifier: impl Into<Cow<'static, str>>,
) -> Self {
NotFoundError {
resource_type: resource_type.into(),
identifier: identifier.into(),
}
}
}
impl fmt::Display for NotFoundError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} not found: {}", self.resource_type, self.identifier)
}
}
impl std::error::Error for NotFoundError {}
/// Invalid parameter error.
#[derive(Debug)]
pub struct InvalidParameterError {
/// Name of the parameter.
pub parameter: Cow<'static, str>,
/// Why the parameter is invalid.
pub reason: Cow<'static, str>,
}
impl InvalidParameterError {
/// Create a new invalid parameter error.
pub fn new(
parameter: impl Into<Cow<'static, str>>,
reason: impl Into<Cow<'static, str>>,
) -> Self {
InvalidParameterError {
parameter: parameter.into(),
reason: reason.into(),
}
}
}
impl fmt::Display for InvalidParameterError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Invalid parameter '{}': {}", self.parameter, self.reason)
}
}
impl std::error::Error for InvalidParameterError {}
/// File operation error.
#[derive(Debug)]
pub struct FileOperationError {
/// The file path.
pub path: Cow<'static, str>,
/// The operation that failed.
pub operation: Cow<'static, str>,
/// Windows error code if available.
pub error_code: Option<i32>,
}
impl FileOperationError {
/// Create a new file operation error.
pub fn new(
path: impl Into<Cow<'static, str>>,
operation: impl Into<Cow<'static, str>>,
) -> Self {
FileOperationError {
path: path.into(),
operation: operation.into(),
error_code: None,
}
}
/// Create a file operation error with an error code.
pub fn with_code(
path: impl Into<Cow<'static, str>>,
operation: impl Into<Cow<'static, str>>,
error_code: i32,
) -> Self {
FileOperationError {
path: path.into(),
operation: operation.into(),
error_code: Some(error_code),
}
}
}
impl fmt::Display for FileOperationError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(code) = self.error_code {
write!(
f,
"File operation '{}' failed on '{}' (error code: 0x{:08X})",
self.operation, self.path, code
)
} else {
write!(
f,
"File operation '{}' failed on '{}'",
self.operation, self.path
)
}
}
}
impl std::error::Error for FileOperationError {}
// ============================================================================
// Pipe Errors
// ============================================================================
/// Pipe-specific errors.
#[derive(Debug)]
pub enum PipeError {
/// Pipe creation failed.
Create(PipeCreateError),
/// Pipe connection failed.
Connect(PipeConnectError),
/// Pipe I/O operation failed.
Io(PipeIoError),
/// Pipe reached timeout.
Timeout(PipeTimeoutError),
/// Pipe is in an invalid state for the operation.
InvalidState(PipeInvalidStateError),
}
impl fmt::Display for PipeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
PipeError::Create(e) => write!(f, "{}", e),
PipeError::Connect(e) => write!(f, "{}", e),
PipeError::Io(e) => write!(f, "{}", e),
PipeError::Timeout(e) => write!(f, "{}", e),
PipeError::InvalidState(e) => write!(f, "{}", e),
}
}
}
impl std::error::Error for PipeError {}
/// Pipe creation error.
#[derive(Debug)]
pub struct PipeCreateError {
/// Pipe name used during creation.
pub pipe_name: Cow<'static, str>,
/// Operation description.
pub operation: Cow<'static, str>,
/// Optional Windows error code.
pub error_code: Option<i32>,
}
impl PipeCreateError {
/// Create a new pipe creation error.
pub fn new(
pipe_name: impl Into<Cow<'static, str>>,
operation: impl Into<Cow<'static, str>>,
) -> Self {
PipeCreateError {
pipe_name: pipe_name.into(),
operation: operation.into(),
error_code: None,
}
}
/// Create a new pipe creation error with an error code.
pub fn with_code(
pipe_name: impl Into<Cow<'static, str>>,
operation: impl Into<Cow<'static, str>>,
error_code: i32,
) -> Self {
PipeCreateError {
pipe_name: pipe_name.into(),
operation: operation.into(),
error_code: Some(error_code),
}
}
}
impl fmt::Display for PipeCreateError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(code) = self.error_code {
write!(
f,
"Pipe creation '{}' failed for '{}' (error code: 0x{:08X})",
self.operation, self.pipe_name, code
)
} else {
write!(
f,
"Pipe creation '{}' failed for '{}'",
self.operation, self.pipe_name
)
}
}
}
impl std::error::Error for PipeCreateError {}
/// Pipe connection error.
#[derive(Debug)]
pub struct PipeConnectError {
/// Pipe name used during connection.
pub pipe_name: Cow<'static, str>,
/// Optional connection context.
pub context: Option<Cow<'static, str>>,
/// Optional Windows error code.
pub error_code: Option<i32>,
}
impl PipeConnectError {
/// Create a new pipe connection error.
pub fn new(pipe_name: impl Into<Cow<'static, str>>) -> Self {
PipeConnectError {
pipe_name: pipe_name.into(),
context: None,
error_code: None,
}
}
/// Add context to a pipe connection error.
pub fn with_context(mut self, context: impl Into<Cow<'static, str>>) -> Self {
self.context = Some(context.into());
self
}
/// Add a Windows error code to a pipe connection error.
pub fn with_code(mut self, error_code: i32) -> Self {
self.error_code = Some(error_code);
self
}
}
impl fmt::Display for PipeConnectError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match (&self.context, self.error_code) {
(Some(context), Some(code)) => write!(
f,
"Pipe connect failed for '{}' ({}, error code: 0x{:08X})",
self.pipe_name, context, code
),
(Some(context), None) => {
write!(
f,
"Pipe connect failed for '{}' ({})",
self.pipe_name, context
)
}
(None, Some(code)) => write!(
f,
"Pipe connect failed for '{}' (error code: 0x{:08X})",
self.pipe_name, code
),
(None, None) => write!(f, "Pipe connect failed for '{}'", self.pipe_name),
}
}
}
impl std::error::Error for PipeConnectError {}
/// Pipe I/O error.
#[derive(Debug)]
pub struct PipeIoError {
/// Pipe name involved in I/O.
pub pipe_name: Cow<'static, str>,
/// I/O operation that failed.
pub operation: Cow<'static, str>,
/// Optional Windows error code.
pub error_code: Option<i32>,
}
impl PipeIoError {
/// Create a new pipe I/O error.
pub fn new(
pipe_name: impl Into<Cow<'static, str>>,
operation: impl Into<Cow<'static, str>>,
) -> Self {
PipeIoError {
pipe_name: pipe_name.into(),
operation: operation.into(),
error_code: None,
}
}
/// Create a new pipe I/O error with an error code.
pub fn with_code(
pipe_name: impl Into<Cow<'static, str>>,
operation: impl Into<Cow<'static, str>>,
error_code: i32,
) -> Self {
PipeIoError {
pipe_name: pipe_name.into(),
operation: operation.into(),
error_code: Some(error_code),
}
}
}
impl fmt::Display for PipeIoError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(code) = self.error_code {
write!(
f,
"Pipe I/O '{}' failed for '{}' (error code: 0x{:08X})",
self.operation, self.pipe_name, code
)
} else {
write!(
f,
"Pipe I/O '{}' failed for '{}'",
self.operation, self.pipe_name
)
}
}
}
impl std::error::Error for PipeIoError {}
/// Pipe timeout error.
#[derive(Debug)]
pub struct PipeTimeoutError {
/// Pipe name that timed out.
pub pipe_name: Cow<'static, str>,
/// Timeout operation.
pub operation: Cow<'static, str>,
}
impl PipeTimeoutError {
/// Create a new pipe timeout error.
pub fn new(
pipe_name: impl Into<Cow<'static, str>>,
operation: impl Into<Cow<'static, str>>,
) -> Self {
PipeTimeoutError {
pipe_name: pipe_name.into(),
operation: operation.into(),
}
}
}
impl fmt::Display for PipeTimeoutError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"Pipe operation '{}' timed out for '{}'",
self.operation, self.pipe_name
)
}
}
impl std::error::Error for PipeTimeoutError {}
/// Pipe invalid-state error.
#[derive(Debug)]
pub struct PipeInvalidStateError {
/// Operation attempted.
pub operation: Cow<'static, str>,
/// Why state is invalid.
pub reason: Cow<'static, str>,
}
impl PipeInvalidStateError {
/// Create a new invalid-state error for a pipe operation.
pub fn new(
operation: impl Into<Cow<'static, str>>,
reason: impl Into<Cow<'static, str>>,
) -> Self {
PipeInvalidStateError {
operation: operation.into(),
reason: reason.into(),
}
}
}
impl fmt::Display for PipeInvalidStateError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"Pipe operation '{}' is invalid in current state: {}",
self.operation, self.reason
)
}
}
impl std::error::Error for PipeInvalidStateError {}
/// Generic error with a message.
#[derive(Debug)]
pub struct OtherError {
/// Error message.
pub message: Cow<'static, str>,
}
impl OtherError {
/// Create a new generic error.
pub fn new(message: impl Into<Cow<'static, str>>) -> Self {
OtherError {
message: message.into(),
}
}
}
impl fmt::Display for OtherError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.message)
}
}
impl std::error::Error for OtherError {}
// ============================================================================
// Desktop Errors
// ============================================================================
/// Desktop and windowing errors.
#[derive(Debug)]
pub enum DesktopError {
/// Desktop operation failed.
OperationFailed(DesktopOperationError),
/// Window not found.
WindowNotFound(WindowNotFoundError),
}
impl fmt::Display for DesktopError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
DesktopError::OperationFailed(e) => write!(f, "{}", e),
DesktopError::WindowNotFound(e) => write!(f, "{}", e),
}
}
}
impl std::error::Error for DesktopError {}
/// Desktop operation failure details.
#[derive(Debug)]
pub struct DesktopOperationError {
/// Operation that failed.
pub operation: Cow<'static, str>,
/// Target resource involved in the operation.
pub target: Cow<'static, str>,
/// Optional Windows error code.
pub error_code: Option<i32>,
}
impl DesktopOperationError {
/// Create a new desktop operation error.
pub fn new(
operation: impl Into<Cow<'static, str>>,
target: impl Into<Cow<'static, str>>,
) -> Self {
DesktopOperationError {
operation: operation.into(),
target: target.into(),
error_code: None,
}
}
/// Create a desktop operation error with a Windows error code.
pub fn with_code(
operation: impl Into<Cow<'static, str>>,
target: impl Into<Cow<'static, str>>,
error_code: i32,
) -> Self {
DesktopOperationError {
operation: operation.into(),
target: target.into(),
error_code: Some(error_code),
}
}
}
impl fmt::Display for DesktopOperationError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(code) = self.error_code {
write!(
f,
"Desktop operation '{}' failed for '{}' (error code: 0x{:08X})",
self.operation, self.target, code
)
} else {
write!(
f,
"Desktop operation '{}' failed for '{}'",
self.operation, self.target
)
}
}
}
impl std::error::Error for DesktopOperationError {}
/// Window not found details.
#[derive(Debug)]
pub struct WindowNotFoundError {
/// The window identifier that was not found.
pub identifier: Cow<'static, str>,
}
impl WindowNotFoundError {
/// Create a new window not found error.
pub fn new(identifier: impl Into<Cow<'static, str>>) -> Self {
WindowNotFoundError {
identifier: identifier.into(),
}
}
}
impl fmt::Display for WindowNotFoundError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Window not found: {}", self.identifier)
}
}
impl std::error::Error for WindowNotFoundError {}
// ============================================================================
// Security Errors
// ============================================================================
/// Security and permissions errors.
#[derive(Debug)]
pub enum SecurityError {
/// SID parsing or encoding failed.
SidParse(SidParseError),
/// Permission edit validation or execution failed.
PermissionEdit(PermissionEditError),
/// Operation is not supported by the current backend/target.
Unsupported(SecurityUnsupportedError),
}
impl fmt::Display for SecurityError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
SecurityError::SidParse(e) => write!(f, "{}", e),
SecurityError::PermissionEdit(e) => write!(f, "{}", e),
SecurityError::Unsupported(e) => write!(f, "{}", e),
}
}
}
impl std::error::Error for SecurityError {}
/// SID parsing error.
#[derive(Debug)]
pub struct SidParseError {
/// Input SID representation that failed.
pub input: Cow<'static, str>,
/// Why parsing failed.
pub reason: Cow<'static, str>,
}
impl SidParseError {
/// Create a new SID parse error.
pub fn new(input: impl Into<Cow<'static, str>>, reason: impl Into<Cow<'static, str>>) -> Self {
SidParseError {
input: input.into(),
reason: reason.into(),
}
}
}
impl fmt::Display for SidParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "SID parse error for '{}': {}", self.input, self.reason)
}
}
impl std::error::Error for SidParseError {}
/// Permission edit error.
#[derive(Debug)]
pub struct PermissionEditError {
/// Operation that failed.
pub operation: Cow<'static, str>,
/// Why it failed.
pub reason: Cow<'static, str>,
}
impl PermissionEditError {
/// Create a new permission edit error.
pub fn new(
operation: impl Into<Cow<'static, str>>,
reason: impl Into<Cow<'static, str>>,
) -> Self {
PermissionEditError {
operation: operation.into(),
reason: reason.into(),
}
}
}
impl fmt::Display for PermissionEditError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"Permission edit '{}' failed: {}",
self.operation, self.reason
)
}
}
impl std::error::Error for PermissionEditError {}
/// Unsupported security operation.
#[derive(Debug)]
pub struct SecurityUnsupportedError {
/// The target/backend where operation was attempted.
pub target: Cow<'static, str>,
/// The unsupported operation.
pub operation: Cow<'static, str>,
/// Optional additional reason.
pub reason: Option<Cow<'static, str>>,
}
impl SecurityUnsupportedError {
/// Create a new unsupported security operation error.
pub fn new(
target: impl Into<Cow<'static, str>>,
operation: impl Into<Cow<'static, str>>,
) -> Self {
SecurityUnsupportedError {
target: target.into(),
operation: operation.into(),
reason: None,
}
}
/// Create a new unsupported security operation error with reason.
pub fn with_reason(
target: impl Into<Cow<'static, str>>,
operation: impl Into<Cow<'static, str>>,
reason: impl Into<Cow<'static, str>>,
) -> Self {
SecurityUnsupportedError {
target: target.into(),
operation: operation.into(),
reason: Some(reason.into()),
}
}
}
impl fmt::Display for SecurityUnsupportedError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(reason) = &self.reason {
write!(
f,
"Security operation '{}' is not supported for '{}': {}",
self.operation, self.target, reason
)
} else {
write!(
f,
"Security operation '{}' is not supported for '{}'",
self.operation, self.target
)
}
}
}
impl std::error::Error for SecurityUnsupportedError {}
// ============================================================================
// Proxy Errors
// ============================================================================
/// Proxy-specific errors.
#[derive(Debug)]
pub enum ProxyError {
/// Proxy configuration is invalid.
InvalidConfig(ProxyConfigError),
/// Proxy discovery operation failed.
DiscoveryFailed(ProxyConfigError),
/// Proxy URL resolution failed.
ResolutionFailed(ProxyResolutionError),
}
impl fmt::Display for ProxyError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ProxyError::InvalidConfig(e) => write!(f, "{}", e),
ProxyError::DiscoveryFailed(e) => write!(f, "{}", e),
ProxyError::ResolutionFailed(e) => write!(f, "{}", e),
}
}
}
impl std::error::Error for ProxyError {}
/// Invalid or unavailable proxy configuration.
#[derive(Debug)]
pub struct ProxyConfigError {
/// Name of the relevant setting.
pub setting: Cow<'static, str>,
/// Reason the setting is invalid or unavailable.
pub reason: Cow<'static, str>,
}
impl ProxyConfigError {
/// Create a new proxy configuration error.
pub fn new(
setting: impl Into<Cow<'static, str>>,
reason: impl Into<Cow<'static, str>>,
) -> Self {
ProxyConfigError {
setting: setting.into(),
reason: reason.into(),
}
}
}
impl fmt::Display for ProxyConfigError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"Proxy configuration error for '{}': {}",
self.setting, self.reason
)
}
}
impl std::error::Error for ProxyConfigError {}
/// URL-specific proxy resolution error.
#[derive(Debug)]
pub struct ProxyResolutionError {
/// URL that was being resolved.
pub url: Cow<'static, str>,
/// Why resolution failed.
pub reason: Cow<'static, str>,
}
impl ProxyResolutionError {
/// Create a new proxy resolution error.
pub fn new(url: impl Into<Cow<'static, str>>, reason: impl Into<Cow<'static, str>>) -> Self {
ProxyResolutionError {
url: url.into(),
reason: reason.into(),
}
}
}
impl fmt::Display for ProxyResolutionError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"Proxy resolution failed for '{}': {}",
self.url, self.reason
)
}
}
impl std::error::Error for ProxyResolutionError {}
// ============================================================================
// Registry Errors
// ============================================================================
/// Registry-specific errors.
#[derive(Debug)]
pub enum RegistryError {
/// Registry key not found.
KeyNotFound(RegistryKeyNotFoundError),
/// Registry value not found.
ValueNotFound(RegistryValueNotFoundError),
/// Invalid value type.
InvalidType(RegistryInvalidTypeError),
/// Error converting value.
ConversionError(RegistryConversionError),
}