@@ -75,10 +75,13 @@ mod fetch {
75
75
use wasm_bindgen_futures:: JsFuture ;
76
76
use web_sys:: { window, RequestInit } ;
77
77
78
- use std:: io;
79
78
use std:: iter:: { IntoIterator , Iterator } ;
80
79
use std:: pin:: Pin ;
81
80
81
+ use http_types:: StatusCode ;
82
+
83
+ use crate :: Error ;
84
+
82
85
/// Create a new fetch request.
83
86
84
87
/// An HTTP Fetch Request.
@@ -91,7 +94,7 @@ mod fetch {
91
94
92
95
impl Request {
93
96
/// Create a new instance.
94
- pub ( crate ) async fn new ( mut req : super :: Request ) -> Result < Self , io :: Error > {
97
+ pub ( crate ) async fn new ( mut req : super :: Request ) -> Result < Self , Error > {
95
98
// create a fetch request initaliser
96
99
let mut init = RequestInit :: new ( ) ;
97
100
@@ -106,7 +109,7 @@ mod fetch {
106
109
// js is just a portal into WASM linear memory, and if the underlying data is moved the
107
110
// js ref will become silently invalid
108
111
let body_buf = body. into_bytes ( ) . await . map_err ( |_| {
109
- io :: Error :: new ( io :: ErrorKind :: Other , "could not read body into a buffer" )
112
+ Error :: from_str ( StatusCode :: BadRequest , "could not read body into a buffer" )
110
113
} ) ?;
111
114
let body_pinned = Pin :: new ( body_buf) ;
112
115
if body_pinned. len ( ) > 0 {
@@ -115,8 +118,8 @@ mod fetch {
115
118
}
116
119
117
120
let request = web_sys:: Request :: new_with_str_and_init ( & uri, & init) . map_err ( |e| {
118
- io :: Error :: new (
119
- io :: ErrorKind :: Other ,
121
+ Error :: from_str (
122
+ StatusCode :: BadRequest ,
120
123
format ! ( "failed to create request: {:?}" , e) ,
121
124
)
122
125
} ) ?;
@@ -128,8 +131,8 @@ mod fetch {
128
131
let value = value. as_str ( ) ;
129
132
130
133
request. headers ( ) . set ( name, value) . map_err ( |_| {
131
- io :: Error :: new (
132
- io :: ErrorKind :: Other ,
134
+ Error :: from_str (
135
+ StatusCode :: BadRequest ,
133
136
format ! ( "could not add header: {} = {}" , name, value) ,
134
137
)
135
138
} ) ?;
@@ -143,11 +146,14 @@ mod fetch {
143
146
144
147
/// Submit a request
145
148
// TODO(yoshuawuyts): turn this into a `Future` impl on `Request` instead.
146
- pub ( crate ) async fn send ( self ) -> Result < Response , io :: Error > {
149
+ pub ( crate ) async fn send ( self ) -> Result < Response , Error > {
147
150
// Send the request.
148
151
let window = window ( ) . expect ( "A global window object could not be found" ) ;
149
152
let promise = window. fetch_with_request ( & self . request ) ;
150
- let resp = JsFuture :: from ( promise) . await . unwrap ( ) ;
153
+ let resp = JsFuture :: from ( promise)
154
+ . await
155
+ . map_err ( |e| Error :: from_str ( StatusCode :: BadRequest , format ! ( "{:?}" , e) ) ) ?;
156
+
151
157
debug_assert ! ( resp. is_instance_of:: <web_sys:: Response >( ) ) ;
152
158
let res: web_sys:: Response = resp. dyn_into ( ) . unwrap ( ) ;
153
159
0 commit comments