@@ -67,8 +67,32 @@ async fn edge_tunnel_addr(server: &str, token: &str) -> Result<SocketAddr> {
6767
6868// ── Channel construction ─────────────────────────────────────────────
6969
70+ /// A failure while preparing or opening a gateway channel.
71+ #[ derive( Debug , thiserror:: Error ) ]
72+ #[ non_exhaustive]
73+ pub enum ChannelBuildError {
74+ /// Endpoint, authentication tunnel, or TLS setup failed.
75+ #[ error( transparent) ]
76+ Setup ( #[ from] SdkError ) ,
77+ /// The transport failed to connect after setup.
78+ #[ error( "gateway transport failed" ) ]
79+ Transport ( #[ from] tonic:: transport:: Error ) ,
80+ }
81+
82+ /// Open a gRPC channel using the SDK's standard error type.
83+ pub async fn build_channel ( config : & ClientConfig ) -> Result < Channel > {
84+ build_channel_with_source ( config)
85+ . await
86+ . map_err ( |error| match error {
87+ ChannelBuildError :: Setup ( error) => error,
88+ ChannelBuildError :: Transport ( source) => SdkError :: connect ( source. to_string ( ) ) ,
89+ } )
90+ }
91+
7092/// Open a gRPC channel to the gateway described by `config`.
7193///
94+ /// Transport failures retain the underlying error chain for diagnostics.
95+ ///
7296/// Routing is determined by `gateway` scheme + `auth` variant +
7397/// `insecure_skip_verify`.
7498///
@@ -80,21 +104,24 @@ async fn edge_tunnel_addr(server: &str, token: &str) -> Result<SocketAddr> {
80104/// | `https://` | `Some(EdgeJwt)` | (any) | tunnel proxy + plaintext to local proxy |
81105/// | `https://` | (any) | `true` | `InsecureTlsConnector`, no verification |
82106/// | `https://` | `Some(Oidc)` or `None` | `false` | tonic TLS, optionally pin `ca_cert` and present `client_identity` |
83- pub async fn build_channel ( config : & ClientConfig ) -> Result < Channel > {
107+ pub async fn build_channel_with_source (
108+ config : & ClientConfig ,
109+ ) -> std:: result:: Result < Channel , ChannelBuildError > {
84110 let gateway = & config. gateway ;
85111
86112 // Branch 1 — plaintext.
87113 if gateway. starts_with ( "http://" ) {
88114 return standard_endpoint ( gateway. clone ( ) ) ?
89115 . connect ( )
90116 . await
91- . map_err ( |e| SdkError :: connect ( format ! ( "{e}" ) ) ) ;
117+ . map_err ( ChannelBuildError :: from ) ;
92118 }
93119
94120 if !gateway. starts_with ( "https://" ) {
95121 return Err ( SdkError :: invalid_config ( format ! (
96122 "gateway URL must start with http:// or https://: {gateway}"
97- ) ) ) ;
123+ ) )
124+ . into ( ) ) ;
98125 }
99126
100127 // Branch 2 — Cloudflare Access edge JWT: tunnel proxy + plaintext-to-local.
@@ -103,7 +130,7 @@ pub async fn build_channel(config: &ClientConfig) -> Result<Channel> {
103130 return standard_endpoint ( format ! ( "http://{local_addr}" ) ) ?
104131 . connect ( )
105132 . await
106- . map_err ( |e| SdkError :: connect ( format ! ( "{e}" ) ) ) ;
133+ . map_err ( ChannelBuildError :: from ) ;
107134 }
108135
109136 // Branch 3 — insecure TLS (skip cert verification): swap the scheme to
@@ -118,7 +145,7 @@ pub async fn build_channel(config: &ClientConfig) -> Result<Channel> {
118145 return standard_endpoint ( http_uri) ?
119146 . connect_with_connector ( connector)
120147 . await
121- . map_err ( |e| SdkError :: connect ( format ! ( "{e}" ) ) ) ;
148+ . map_err ( ChannelBuildError :: from ) ;
122149 }
123150
124151 // Branch 4 — anonymous TLS or OIDC bearer over HTTPS: pin `ca_cert` when
@@ -141,7 +168,7 @@ pub async fn build_channel(config: &ClientConfig) -> Result<Channel> {
141168 . map_err ( |e| SdkError :: tls ( format ! ( "{e}" ) ) ) ?
142169 . connect ( )
143170 . await
144- . map_err ( |e| SdkError :: connect ( format ! ( "{e}" ) ) )
171+ . map_err ( ChannelBuildError :: from )
145172}
146173
147174/// rustls verifier that accepts any server certificate.
0 commit comments