1+ import {
2+ CoreCryptoError_Tags ,
3+ CoreCryptoError as CoreCryptoErrorFfi ,
4+ MlsError as MlsErrorFfi ,
5+ ProteusError as ProteusErrorFfi ,
6+ MlsError_Tags ,
7+ ProteusError_Tags ,
8+ } from "./index.web" ;
9+
110/**
211 * The error structure produced by our rust code.
312 **/
@@ -23,58 +32,146 @@ export class CoreCryptoError<T extends ErrorType> extends Error {
2332 context ?: ErrorContext [ T ] ;
2433 type ?: T ;
2534
26- /* eslint @typescript-eslint/no-explicit-any: off */
27- private constructor ( richError : CoreCryptoRichError < T > , ...params : any [ ] ) {
28- super ( richError . message , ...params ) ;
29- Object . setPrototypeOf ( this , new . target . prototype ) ;
35+ private constructor ( message : string , type ?: T , context ?: ErrorContext [ T ] ) {
36+ super ( message ) ;
37+ this . type = type ;
38+ this . context = context ;
39+ this . errorStack = [ ] ;
40+ }
3041
31- if ( richError . error_name ) {
32- this . name = richError . error_name ;
33- }
34- if ( richError . error_stack ) {
35- this . errorStack = richError . error_stack ;
42+ private static fromStdError ( error : unknown ) : CoreCryptoError < ErrorType > {
43+ if ( CoreCryptoErrorFfi . instanceOf ( error ) ) {
44+ switch ( error . tag ) {
45+ case CoreCryptoError_Tags . E2ei :
46+ return new CoreCryptoError ( error . message , ErrorType . E2ei , {
47+ e2eiError : error . inner . e2eiError ,
48+ } ) ;
49+ case CoreCryptoError_Tags . Mls :
50+ return CoreCryptoError . fromMlsError ( error . inner . mlsError ) ;
51+ case CoreCryptoError_Tags . Proteus :
52+ return CoreCryptoError . fromProteusError (
53+ error . inner . exception
54+ ) ;
55+ case CoreCryptoError_Tags . Other :
56+ return new CoreCryptoError ( error . message , ErrorType . Other , {
57+ msg : error . inner . msg ,
58+ } ) ;
59+ case CoreCryptoError_Tags . TransactionFailed :
60+ return new CoreCryptoError (
61+ error . message ,
62+ ErrorType . TransactionFailed ,
63+ { error : error . inner . error }
64+ ) ;
65+ }
3666 } else {
37- this . errorStack = [ ] ;
67+ throw Error (
68+ "Unexpected error instance. Context: constructing CoreCryptoError."
69+ ) ;
3870 }
39- if (
40- richError . context &&
41- richError . type &&
42- Object . values < string > ( ErrorType ) . includes ( richError . type )
43- ) {
44- this . context = richError . context ;
45- this . type = richError . type as T ;
46- }
47- }
48-
49- private static fallback < E extends ErrorType > (
50- message : string ,
51- ...params : any [ ]
52- ) : CoreCryptoError < E > {
53- return new CoreCryptoError ( { message } , ...params ) ;
5471 }
5572
56- static build < E extends ErrorType > (
57- msg : string ,
58- ...params : unknown [ ]
59- ) : CoreCryptoError < E > {
60- try {
61- const richError : CoreCryptoRichError < E > = JSON . parse ( msg ) ;
62- return new this ( richError , ...params ) ;
63- } catch {
64- return this . fallback ( msg , ...params ) ;
73+ private static fromMlsError (
74+ error : MlsErrorFfi
75+ ) : CoreCryptoError < ErrorType . Mls > {
76+ switch ( error . tag ) {
77+ case MlsError_Tags . BufferedCommit :
78+ return new CoreCryptoError ( error . message , ErrorType . Mls , {
79+ type : MlsErrorType . BufferedCommit ,
80+ context : { } ,
81+ } ) ;
82+
83+ case MlsError_Tags . BufferedFutureMessage :
84+ return new CoreCryptoError ( error . message , ErrorType . Mls , {
85+ type : MlsErrorType . BufferedFutureMessage ,
86+ context : { } ,
87+ } ) ;
88+
89+ case MlsError_Tags . ConversationAlreadyExists :
90+ return new CoreCryptoError ( error . message , ErrorType . Mls , {
91+ type : MlsErrorType . ConversationAlreadyExists ,
92+ context : {
93+ conversationId : error . inner . conversationId ,
94+ } ,
95+ } ) ;
96+ case MlsError_Tags . DuplicateMessage :
97+ return new CoreCryptoError ( error . message , ErrorType . Mls , {
98+ type : MlsErrorType . DuplicateMessage ,
99+ context : { } ,
100+ } ) ;
101+
102+ case MlsError_Tags . MessageEpochTooOld :
103+ return new CoreCryptoError ( error . message , ErrorType . Mls , {
104+ type : MlsErrorType . MessageEpochTooOld ,
105+ context : { } ,
106+ } ) ;
107+ case MlsError_Tags . MessageRejected :
108+ return new CoreCryptoError ( error . message , ErrorType . Mls , {
109+ type : MlsErrorType . MessageRejected ,
110+ context : { reason : error . inner . reason } ,
111+ } ) ;
112+ case MlsError_Tags . OrphanWelcome :
113+ return new CoreCryptoError ( error . message , ErrorType . Mls , {
114+ type : MlsErrorType . OrphanWelcome ,
115+ context : { } ,
116+ } ) ;
117+ case MlsError_Tags . Other :
118+ return new CoreCryptoError ( error . message , ErrorType . Mls , {
119+ type : MlsErrorType . Other ,
120+ context : { msg : error . inner . msg } ,
121+ } ) ;
122+ case MlsError_Tags . SelfCommitIgnored :
123+ return new CoreCryptoError ( error . message , ErrorType . Mls , {
124+ type : MlsErrorType . SelfCommitIgnored ,
125+ context : { } ,
126+ } ) ;
127+ case MlsError_Tags . StaleCommit :
128+ return new CoreCryptoError ( error . message , ErrorType . Mls , {
129+ type : MlsErrorType . StaleCommit ,
130+ context : { } ,
131+ } ) ;
132+ case MlsError_Tags . StaleProposal :
133+ return new CoreCryptoError ( error . message , ErrorType . Mls , {
134+ type : MlsErrorType . StaleProposal ,
135+ context : { } ,
136+ } ) ;
137+ case MlsError_Tags . UnmergedPendingGroup :
138+ return new CoreCryptoError ( error . message , ErrorType . Mls , {
139+ type : MlsErrorType . UnmergedPendingGroup ,
140+ context : { } ,
141+ } ) ;
142+ case MlsError_Tags . WrongEpoch :
143+ return new CoreCryptoError ( error . message , ErrorType . Mls , {
144+ type : MlsErrorType . WrongEpoch ,
145+ context : { } ,
146+ } ) ;
65147 }
66148 }
67149
68- static fromStdError ( e : Error ) : CoreCryptoError < ErrorType > {
69- if ( isCcErrorGeneric ( e ) ) {
70- return e ;
150+ private static fromProteusError (
151+ error : ProteusErrorFfi
152+ ) : CoreCryptoError < ErrorType . Proteus > {
153+ switch ( error . tag ) {
154+ case ProteusError_Tags . DuplicateMessage :
155+ return new CoreCryptoError ( error . message , ErrorType . Proteus , {
156+ type : ProteusErrorType . DuplicateMessage ,
157+ context : { } ,
158+ } ) ;
159+ case ProteusError_Tags . Other :
160+ return new CoreCryptoError ( error . message , ErrorType . Proteus , {
161+ type : ProteusErrorType . Other ,
162+ context : { errorCode : error . inner . errorCode } ,
163+ } ) ;
164+ case ProteusError_Tags . RemoteIdentityChanged :
165+ return new CoreCryptoError ( error . message , ErrorType . Proteus , {
166+ type : ProteusErrorType . RemoteIdentityChanged ,
167+ context : { } ,
168+ } ) ;
169+ case ProteusError_Tags . SessionNotFound :
170+ return new CoreCryptoError ( error . message , ErrorType . Proteus , {
171+ type : ProteusErrorType . SessionNotFound ,
172+ context : { } ,
173+ } ) ;
71174 }
72- const opts = {
73- cause : e . cause || undefined ,
74- stack : e . stack || undefined ,
75- } ;
76-
77- return this . build ( e . message , opts ) ;
78175 }
79176
80177 static async asyncMapErr < T , E extends ErrorType > (
@@ -185,7 +282,7 @@ export enum MlsErrorType {
185282 * Structured core crypto mls error (embedded in a core crypto error)
186283 */
187284export interface MlsErrorContext {
188- [ MlsErrorType . ConversationAlreadyExists ] : { conversationId : Array < number > } ;
285+ [ MlsErrorType . ConversationAlreadyExists ] : { conversationId : ArrayBuffer } ;
189286 [ MlsErrorType . DuplicateMessage ] : Record < string , never > ;
190287 [ MlsErrorType . BufferedFutureMessage ] : Record < string , never > ;
191288 [ MlsErrorType . WrongEpoch ] : Record < string , never > ;
@@ -356,9 +453,9 @@ export enum ProteusErrorType {
356453 * Structured core crypto proteus error (embedded in a core crypto error)
357454 */
358455export interface ProteusErrorContext {
359- [ ProteusErrorType . SessionNotFound ] : { errorCode : number } ;
360- [ ProteusErrorType . DuplicateMessage ] : { errorCode : number } ;
361- [ ProteusErrorType . RemoteIdentityChanged ] : { errorCode : number } ;
456+ [ ProteusErrorType . SessionNotFound ] : Record < string , never > ;
457+ [ ProteusErrorType . DuplicateMessage ] : Record < string , never > ;
458+ [ ProteusErrorType . RemoteIdentityChanged ] : Record < string , never > ;
362459 [ ProteusErrorType . Other ] : { errorCode : number } ;
363460}
364461
0 commit comments