1+ const ApiError = require ( './lib/error' ) ;
2+
13const collections = require ( './lib/collections' ) ;
24const models = require ( './lib/models' ) ;
35const predictions = require ( './lib/predictions' ) ;
46const trainings = require ( './lib/trainings' ) ;
7+
58const packageJSON = require ( './package.json' ) ;
69
710/**
@@ -132,7 +135,7 @@ class Replicate {
132135 * @param {object|Headers } [options.headers] - HTTP headers
133136 * @param {object } [options.data] - Body parameters
134137 * @returns {Promise<Response> } - Resolves with the response object
135- * @throws {Error } If the request failed
138+ * @throws {ApiError } If the request failed
136139 */
137140 async request ( route , options ) {
138141 const { auth, baseUrl, userAgent } = this ;
@@ -163,14 +166,22 @@ class Replicate {
163166 } ) ;
164167 }
165168
166- const response = await this . fetch ( url , {
169+ const init = {
167170 method,
168171 headers,
169172 body : data ? JSON . stringify ( data ) : undefined ,
170- } ) ;
173+ } ;
174+
175+ const response = await this . fetch ( url , init ) ;
171176
172177 if ( ! response . ok ) {
173- throw new Error ( `API request failed: ${ response . statusText } ` ) ;
178+ const request = new Request ( url , init ) ;
179+ const responseText = await response . text ( ) ;
180+ throw new ApiError (
181+ `Request to ${ url } failed with status ${ response . status } ${ response . statusText } : ${ responseText } .` ,
182+ request ,
183+ response ,
184+ ) ;
174185 }
175186
176187 return response ;
@@ -187,7 +198,7 @@ class Replicate {
187198 * @param {Function } endpoint - Function that returns a promise for the next page of results
188199 * @yields {object[]} Each page of results
189200 */
190- async * paginate ( endpoint ) {
201+ async * paginate ( endpoint ) {
191202 const response = await endpoint ( ) ;
192203 yield response . results ;
193204 if ( response . next ) {
0 commit comments