11// Copyright (c) Microsoft Corporation.
22// Licensed under the MIT License.
33
4+ use std:: borrow:: Cow ;
5+
46use http:: uri:: { InvalidUri , InvalidUriParts } ;
7+ use ohno:: { ErrorLabel , Labeled } ;
8+
9+ const LABEL_URI_INVALID : ErrorLabel = ErrorLabel :: from_static ( "uri_invalid" ) ;
10+ const LABEL_URI_HTTP_ERROR : ErrorLabel = ErrorLabel :: from_static ( "uri_http_error" ) ;
511
612/// Represents errors that occur during URI validation.
713///
814/// This error type is returned when URI parsing or validation fails,
915/// typically due to malformed syntax or invalid URI components. It can wrap
1016/// errors from the `http` crate, such as `InvalidUri` and `InvalidUriParts`,
11- /// and exposes them via the `source()` method as `http::Error` when available .
17+ /// and exposes them via the `source()` method.
1218#[ ohno:: error]
13- #[ from( http:: Error ) ]
14- pub struct ValidationError ;
19+ #[ from( http:: Error ( label: LABEL_URI_HTTP_ERROR ) ) ]
20+ #[ from( InvalidUri ( label: LABEL_URI_INVALID ) ) ]
21+ #[ from( InvalidUriParts ( label: LABEL_URI_INVALID ) ) ]
22+ pub struct ValidationError {
23+ label : ErrorLabel ,
24+ }
1525
16- /// `InvalidUri` is a flavor of `http::Error`
17- impl From < InvalidUri > for ValidationError {
18- fn from ( err : InvalidUri ) -> Self {
19- Self :: from ( http:: Error :: from ( err) )
26+ impl ValidationError {
27+ pub ( crate ) fn invalid_uri ( message : impl Into < Cow < ' static , str > > ) -> Self {
28+ Self :: caused_by ( LABEL_URI_INVALID , message. into ( ) )
2029 }
2130}
2231
23- /// `InvalidUriParts` is a flavor of `http::Error`
24- impl From < InvalidUriParts > for ValidationError {
25- fn from ( err : InvalidUriParts ) -> Self {
26- Self :: from ( http:: Error :: from ( err) )
32+ impl Labeled for ValidationError {
33+ fn label ( & self ) -> & ErrorLabel {
34+ & self . label
2735 }
2836}
2937
3038#[ cfg( test) ]
3139mod tests {
3240 use std:: error:: Error ;
3341
42+ use ohno:: { ErrorLabel , Labeled } ;
43+
3444 use super :: ValidationError ;
3545
3646 #[ test]
3747 fn test_error_display ( ) {
38- let error = ValidationError :: caused_by ( "Test validation error" ) ;
48+ let error = ValidationError :: caused_by ( ErrorLabel :: from_static ( "test" ) , "Test validation error" ) ;
3949 let display = error. to_string ( ) ;
4050 assert ! ( display. starts_with( "Test validation error" ) , "Unexpected message: {display}" ) ;
4151 }
4252
4353 #[ test]
4454 fn test_source ( ) {
45- let error = ValidationError :: caused_by ( "Test validation error" ) ;
55+ let error = ValidationError :: caused_by ( ErrorLabel :: from_static ( "test" ) , "Test validation error" ) ;
4656 assert ! ( error. source( ) . is_none( ) ) ;
4757 }
4858
4959 #[ test]
5060 fn test_from_invalid_uri ( ) {
5161 // Create an invalid URI error
52- let invalid_uri_result = "http://[::1:invalid" . parse :: < http:: Uri > ( ) ;
53- assert ! ( invalid_uri_result. is_err( ) ) ;
54-
55- let invalid_uri = invalid_uri_result. unwrap_err ( ) ;
62+ let invalid_uri = "http://[::1:invalid" . parse :: < http:: Uri > ( ) . unwrap_err ( ) ;
5663 let validation_error = ValidationError :: from ( invalid_uri) ;
5764
5865 // Verify the error can be displayed
5966 let display = validation_error. to_string ( ) ;
6067 assert ! ( !display. is_empty( ) , "Error should have a non-empty display" ) ;
68+ assert_eq ! ( validation_error. label( ) , "uri_invalid" ) ;
6169
6270 // Verify it has a source
6371 assert ! ( validation_error. source( ) . is_some( ) , "Should have a source error" ) ;
@@ -76,6 +84,7 @@ mod tests {
7684 let display = validation_error. to_string ( ) ;
7785 assert ! ( !display. is_empty( ) , "Error should have a non-empty display" ) ;
7886 assert ! ( validation_error. source( ) . is_some( ) , "Should have a source error" ) ;
87+ assert_eq ! ( validation_error. label( ) , "uri_http_error" ) ;
7988 }
8089
8190 #[ test]
@@ -97,5 +106,6 @@ mod tests {
97106 let display = validation_error. to_string ( ) ;
98107 assert ! ( !display. is_empty( ) , "Error should have a non-empty display" ) ;
99108 assert ! ( validation_error. source( ) . is_some( ) , "Should have a source error" ) ;
109+ assert_eq ! ( validation_error. label( ) , "uri_invalid" ) ;
100110 }
101111}
0 commit comments