@@ -11,7 +11,7 @@ import type {
1111 CommentRow ,
1212 CommentStatus ,
1313} from '@open-codesign/shared' ;
14- import { CodesignError } from '@open-codesign/shared' ;
14+ import { CodesignError , ERROR_CODES } from '@open-codesign/shared' ;
1515import type BetterSqlite3 from 'better-sqlite3' ;
1616import { ipcMain } from './electron-runtime' ;
1717import { getLogger } from './logger' ;
@@ -33,36 +33,42 @@ const VALID_STATUSES: CommentStatus[] = ['pending', 'applied', 'dismissed'];
3333
3434function requireSchemaV1 ( r : Record < string , unknown > , channel : string ) : void {
3535 if ( r [ 'schemaVersion' ] !== 1 ) {
36- throw new CodesignError ( `${ channel } requires schemaVersion: 1` , ' IPC_BAD_INPUT' ) ;
36+ throw new CodesignError ( `${ channel } requires schemaVersion: 1` , ERROR_CODES . IPC_BAD_INPUT ) ;
3737 }
3838}
3939
4040function asObject ( raw : unknown , channel : string ) : Record < string , unknown > {
4141 if ( typeof raw !== 'object' || raw === null ) {
42- throw new CodesignError ( `${ channel } expects an object payload` , ' IPC_BAD_INPUT' ) ;
42+ throw new CodesignError ( `${ channel } expects an object payload` , ERROR_CODES . IPC_BAD_INPUT ) ;
4343 }
4444 return raw as Record < string , unknown > ;
4545}
4646
4747function parseNonEmptyString ( r : Record < string , unknown > , field : string , channel : string ) : string {
4848 const v = r [ field ] ;
4949 if ( typeof v !== 'string' || v . trim ( ) . length === 0 ) {
50- throw new CodesignError ( `${ channel } : ${ field } must be a non-empty string` , 'IPC_BAD_INPUT' ) ;
50+ throw new CodesignError (
51+ `${ channel } : ${ field } must be a non-empty string` ,
52+ ERROR_CODES . IPC_BAD_INPUT ,
53+ ) ;
5154 }
5255 return v ;
5356}
5457
5558function parseRect ( raw : unknown , channel : string ) : CommentCreateInput [ 'rect' ] {
5659 if ( typeof raw !== 'object' || raw === null ) {
57- throw new CodesignError ( `${ channel } : rect must be an object` , ' IPC_BAD_INPUT' ) ;
60+ throw new CodesignError ( `${ channel } : rect must be an object` , ERROR_CODES . IPC_BAD_INPUT ) ;
5861 }
5962 const r = raw as Record < string , unknown > ;
6063 const fields = [ 'top' , 'left' , 'width' , 'height' ] as const ;
6164 const out : Record < string , number > = { } ;
6265 for ( const f of fields ) {
6366 const v = r [ f ] ;
6467 if ( typeof v !== 'number' || ! Number . isFinite ( v ) ) {
65- throw new CodesignError ( `${ channel } : rect.${ f } must be a finite number` , 'IPC_BAD_INPUT' ) ;
68+ throw new CodesignError (
69+ `${ channel } : rect.${ f } must be a finite number` ,
70+ ERROR_CODES . IPC_BAD_INPUT ,
71+ ) ;
6672 }
6773 out [ f ] = v ;
6874 }
@@ -83,30 +89,33 @@ function parseAddInput(raw: unknown): CommentCreateInput {
8389 if ( typeof kind !== 'string' || ! VALID_KINDS . includes ( kind as CommentKind ) ) {
8490 throw new CodesignError (
8591 `${ channel } : kind must be one of ${ VALID_KINDS . join ( ', ' ) } ` ,
86- ' IPC_BAD_INPUT' ,
92+ ERROR_CODES . IPC_BAD_INPUT ,
8793 ) ;
8894 }
8995 const text = r [ 'text' ] ;
9096 if ( typeof text !== 'string' ) {
91- throw new CodesignError ( `${ channel } : text must be a string` , ' IPC_BAD_INPUT' ) ;
97+ throw new CodesignError ( `${ channel } : text must be a string` , ERROR_CODES . IPC_BAD_INPUT ) ;
9298 }
9399 const outerHTML = r [ 'outerHTML' ] ;
94100 if ( typeof outerHTML !== 'string' ) {
95- throw new CodesignError ( `${ channel } : outerHTML must be a string` , ' IPC_BAD_INPUT' ) ;
101+ throw new CodesignError ( `${ channel } : outerHTML must be a string` , ERROR_CODES . IPC_BAD_INPUT ) ;
96102 }
97103 const scopeRaw = r [ 'scope' ] ;
98104 let scope : 'element' | 'global' = 'element' ;
99105 if ( scopeRaw !== undefined ) {
100106 if ( scopeRaw !== 'element' && scopeRaw !== 'global' ) {
101- throw new CodesignError ( `${ channel } : scope must be 'element' or 'global'` , 'IPC_BAD_INPUT' ) ;
107+ throw new CodesignError (
108+ `${ channel } : scope must be 'element' or 'global'` ,
109+ ERROR_CODES . IPC_BAD_INPUT ,
110+ ) ;
102111 }
103112 scope = scopeRaw ;
104113 }
105114 const parentRaw = r [ 'parentOuterHTML' ] ;
106115 if ( parentRaw !== undefined && parentRaw !== null && typeof parentRaw !== 'string' ) {
107116 throw new CodesignError (
108117 `${ channel } : parentOuterHTML must be a string when present` ,
109- ' IPC_BAD_INPUT' ,
118+ ERROR_CODES . IPC_BAD_INPUT ,
110119 ) ;
111120 }
112121 const parentOuterHTML =
@@ -150,7 +159,7 @@ export function registerCommentsIpc(db: Database): void {
150159 designId : input . designId ,
151160 message : err instanceof Error ? err . message : String ( err ) ,
152161 } ) ;
153- throw new CodesignError ( 'Failed to create comment' , ' IPC_DB_ERROR' , { cause : err } ) ;
162+ throw new CodesignError ( 'Failed to create comment' , ERROR_CODES . IPC_DB_ERROR , { cause : err } ) ;
154163 }
155164 } ) ;
156165
@@ -163,7 +172,7 @@ export function registerCommentsIpc(db: Database): void {
163172 if ( snapshotId !== undefined && snapshotId !== null && typeof snapshotId !== 'string' ) {
164173 throw new CodesignError (
165174 `${ channel } : snapshotId must be a string, null, or absent` ,
166- ' IPC_BAD_INPUT' ,
175+ ERROR_CODES . IPC_BAD_INPUT ,
167176 ) ;
168177 }
169178 return typeof snapshotId === 'string'
@@ -184,7 +193,7 @@ export function registerCommentsIpc(db: Database): void {
184193 const patch : { text ?: string ; status ?: CommentStatus } = { } ;
185194 if ( r [ 'text' ] !== undefined ) {
186195 if ( typeof r [ 'text' ] !== 'string' ) {
187- throw new CodesignError ( `${ channel } : text must be a string` , ' IPC_BAD_INPUT' ) ;
196+ throw new CodesignError ( `${ channel } : text must be a string` , ERROR_CODES . IPC_BAD_INPUT ) ;
188197 }
189198 patch . text = r [ 'text' ] ;
190199 }
@@ -193,7 +202,7 @@ export function registerCommentsIpc(db: Database): void {
193202 if ( typeof s !== 'string' || ! VALID_STATUSES . includes ( s as CommentStatus ) ) {
194203 throw new CodesignError (
195204 `${ channel } : status must be one of ${ VALID_STATUSES . join ( ', ' ) } ` ,
196- ' IPC_BAD_INPUT' ,
205+ ERROR_CODES . IPC_BAD_INPUT ,
197206 ) ;
198207 }
199208 patch . status = s as CommentStatus ;
@@ -220,7 +229,7 @@ export function registerCommentsIpc(db: Database): void {
220229 if ( ! Array . isArray ( ids ) || ids . some ( ( x ) => typeof x !== 'string' || x . length === 0 ) ) {
221230 throw new CodesignError (
222231 `${ channel } : ids must be an array of non-empty strings` ,
223- ' IPC_BAD_INPUT' ,
232+ ERROR_CODES . IPC_BAD_INPUT ,
224233 ) ;
225234 }
226235 const rows = markCommentsApplied ( db , ids as string [ ] , snapshotId ) ;
@@ -232,7 +241,7 @@ export function registerCommentsIpc(db: Database): void {
232241export function registerCommentsUnavailableIpc ( reason : string ) : void {
233242 const message = `Comments are unavailable. ${ reason } ` ;
234243 const fail = ( ) : never => {
235- throw new CodesignError ( message , ' SNAPSHOTS_UNAVAILABLE' ) ;
244+ throw new CodesignError ( message , ERROR_CODES . SNAPSHOTS_UNAVAILABLE ) ;
236245 } ;
237246 for ( const channel of COMMENTS_CHANNELS_V1 ) {
238247 ipcMain . handle ( channel , fail ) ;
0 commit comments