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/**
@@ -127,7 +130,7 @@ class Replicate {
127130 * @param {object } [parameters.params] - Query parameters
128131 * @param {object } [parameters.data] - Body parameters
129132 * @returns {Promise<object> } - Resolves with the API response data
130- * @throws {Error } If the request failed
133+ * @throws {ApiError } If the request failed
131134 */
132135 async request ( route , parameters ) {
133136 const { auth, baseUrl, userAgent } = this ;
@@ -149,14 +152,22 @@ class Replicate {
149152 'User-Agent' : userAgent ,
150153 } ;
151154
152- const response = await this . fetch ( url , {
155+ const options = {
153156 method,
154157 headers,
155158 body : data ? JSON . stringify ( data ) : undefined ,
156- } ) ;
159+ } ;
160+
161+ const response = await this . fetch ( url , options ) ;
157162
158163 if ( ! response . ok ) {
159- throw new Error ( `API request failed: ${ response . statusText } ` ) ;
164+ const request = new Request ( url , options ) ;
165+ const responseText = await response . text ( ) ;
166+ throw new ApiError (
167+ `Request to ${ url } failed with status ${ response . status } ${ response . statusText } : ${ responseText } .` ,
168+ request ,
169+ response ,
170+ ) ;
160171 }
161172
162173 return response . json ( ) ;
@@ -173,7 +184,7 @@ class Replicate {
173184 * @param {Function } endpoint - Function that returns a promise for the next page of results
174185 * @yields {object[]} Each page of results
175186 */
176- async * paginate ( endpoint ) {
187+ async * paginate ( endpoint ) {
177188 const response = await endpoint ( ) ;
178189 yield response . results ;
179190 if ( response . next ) {
0 commit comments