@@ -14,14 +14,68 @@ export interface RunOptions {
1414 maxRetries ?: number ; // Maximum task-level retries (overrides client setting)
1515}
1616
17+ /**
18+ * Base exception class for WaveSpeed errors
19+ */
20+ export class WavespeedException extends Error {
21+ constructor (
22+ message : string ,
23+ public readonly taskId : string = 'unknown' ,
24+ public readonly model : string = ''
25+ ) {
26+ super ( message ) ;
27+ this . name = 'WavespeedException' ;
28+ }
29+ }
30+
31+ /**
32+ * Timeout exception
33+ */
34+ export class WavespeedTimeoutException extends WavespeedException {
35+ constructor ( taskId : string , model : string , public readonly timeout : number ) {
36+ super ( `Prediction timed out after ${ timeout } seconds` , taskId , model ) ;
37+ this . name = 'WavespeedTimeoutException' ;
38+ }
39+ }
40+
41+ /**
42+ * Connection exception
43+ */
44+ export class WavespeedConnectionException extends WavespeedException {
45+ constructor ( taskId : string , model : string , details : string ) {
46+ super ( `Connection failed: ${ details } ` , taskId , model ) ;
47+ this . name = 'WavespeedConnectionException' ;
48+ }
49+ }
50+
51+ /**
52+ * Prediction failed exception
53+ */
54+ export class WavespeedPredictionException extends WavespeedException {
55+ constructor ( taskId : string , model : string , errorMessage : string ) {
56+ super ( `Prediction failed: ${ errorMessage } ` , taskId , model ) ;
57+ this . name = 'WavespeedPredictionException' ;
58+ }
59+ }
60+
61+ /**
62+ * Unknown exception
63+ */
64+ export class WavespeedUnknownException extends WavespeedException {
65+ constructor ( taskId : string , model : string , originalError : any ) {
66+ super ( String ( originalError ) , taskId , model ) ;
67+ this . name = 'WavespeedUnknownException' ;
68+ }
69+ }
70+
1771/**
1872 * Detail information for runNoThrow result.
1973 */
2074export interface RunDetail {
2175 taskId : string ; // Task ID for tracking and debugging
2276 status : 'completed' | 'failed' ; // Task status
2377 model : string ; // Model identifier
24- error ?: string ; // Error message if failed
78+ error ?: WavespeedException ; // Exception instance if failed
2579 createdAt ?: string ; // Task creation timestamp
2680}
2781
@@ -507,14 +561,14 @@ export class Client {
507561 const taskId = data . id || 'unknown' ;
508562
509563 if ( status !== 'completed' ) {
510- const error = data . error || 'Unknown error' ;
564+ const errorMsg = data . error || 'Unknown error' ;
511565 return {
512566 outputs : null ,
513567 detail : {
514568 taskId,
515569 status : 'failed' ,
516570 model,
517- error,
571+ error : new WavespeedPredictionException ( taskId , model , errorMsg ) ,
518572 createdAt : data . created_at
519573 }
520574 } ;
@@ -550,7 +604,7 @@ export class Client {
550604 taskId : 'unknown' ,
551605 status : 'failed' ,
552606 model,
553- error : ' Invalid response from _submit'
607+ error : new WavespeedUnknownException ( 'unknown' , model , ' Invalid response from _submit')
554608 }
555609 } ;
556610
@@ -563,13 +617,28 @@ export class Client {
563617 const taskIdMatch = error . message ?. match ( / t a s k _ i d : ( [ a - f 0 - 9 - ] + ) / ) ;
564618 const taskId = taskIdMatch ? taskIdMatch [ 1 ] : 'unknown' ;
565619
620+ // Determine exception type based on error
621+ let exception : WavespeedException ;
622+ const errorStr = error . toString ( ) . toLowerCase ( ) ;
623+
624+ if ( errorStr . includes ( 'timeout' ) || errorStr . includes ( 'timed out' ) ) {
625+ exception = new WavespeedTimeoutException ( taskId , model , timeout || 0 ) ;
626+ } else if ( errorStr . includes ( 'connection' ) || errorStr . includes ( 'fetch' ) || error . name === 'AbortError' || error . name === 'TypeError' ) {
627+ exception = new WavespeedConnectionException ( taskId , model , error . message || String ( error ) ) ;
628+ } else if ( errorStr . includes ( 'prediction failed' ) ) {
629+ const errorMsg = error . message ?. replace ( / P r e d i c t i o n f a i l e d \( t a s k _ i d : [ a - f 0 - 9 - ] + \) : / , '' ) || 'Unknown error' ;
630+ exception = new WavespeedPredictionException ( taskId , model , errorMsg ) ;
631+ } else {
632+ exception = new WavespeedUnknownException ( taskId , model , error ) ;
633+ }
634+
566635 return {
567636 outputs : null ,
568637 detail : {
569638 taskId,
570639 status : 'failed' ,
571640 model,
572- error : error . message || String ( error )
641+ error : exception
573642 }
574643 } ;
575644 }
@@ -589,7 +658,7 @@ export class Client {
589658 taskId : 'unknown' ,
590659 status : 'failed' ,
591660 model,
592- error : `All ${ taskRetries + 1 } attempts failed`
661+ error : new WavespeedUnknownException ( 'unknown' , model , `All ${ taskRetries + 1 } attempts failed` )
593662 }
594663 } ;
595664 }
0 commit comments