Skip to content

Commit 982b65c

Browse files
committed
feat: export error contract helpers
1 parent 6cf63c2 commit 982b65c

3 files changed

Lines changed: 24 additions & 4 deletions

File tree

src/__tests__/contracts-schema-public.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
import { test } from 'vitest';
22
import assert from 'node:assert/strict';
3+
import { AppError } from '../index.ts';
34
import {
5+
defaultHintForCode,
46
daemonCommandRequestSchema,
57
daemonRuntimeSchema,
68
jsonRpcRequestSchema,
79
leaseAllocateSchema,
810
leaseHeartbeatSchema,
911
leaseReleaseSchema,
12+
normalizeError,
13+
type AppErrorCode,
1014
} from '../contracts.ts';
1115

16+
const invalidArgsCode = 'INVALID_ARGS' satisfies AppErrorCode;
17+
1218
test('public contract schemas validate daemon requests and lease payloads', () => {
1319
const runtime = daemonRuntimeSchema.parse({
1420
platform: 'ios',
@@ -56,6 +62,17 @@ test('public contract schemas validate daemon requests and lease payloads', () =
5662
assert.equal(release.leaseId, 'lease-1');
5763
});
5864

65+
test('public contract exports normalize and hint app errors', () => {
66+
const normalized = normalizeError(new AppError(invalidArgsCode, 'Invalid command'));
67+
68+
assert.equal(normalized.code, invalidArgsCode);
69+
assert.equal(normalized.hint, defaultHintForCode(invalidArgsCode));
70+
assert.equal(
71+
defaultHintForCode('UNKNOWN'),
72+
'Retry with --debug and inspect diagnostics log for details.',
73+
);
74+
});
75+
5976
test('public contract schemas reject invalid payloads', () => {
6077
assert.throws(
6178
() =>

src/contracts.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
export type { AppErrorCode } from './utils/errors.ts';
2+
export { defaultHintForCode, normalizeError } from './utils/errors.ts';
3+
14
export type SessionRuntimeHints = {
25
platform?: 'ios' | 'android';
36
metroHost?: string;

src/utils/errors.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { redactDiagnosticData } from './diagnostics.ts';
22

3-
type ErrorCode =
3+
export type AppErrorCode =
44
| 'INVALID_ARGS'
55
| 'DEVICE_NOT_FOUND'
66
| 'TOOL_MISSING'
@@ -28,11 +28,11 @@ export type NormalizedError = {
2828
};
2929

3030
export class AppError extends Error {
31-
code: ErrorCode;
31+
code: AppErrorCode;
3232
details?: AppErrorDetails;
3333
cause?: unknown;
3434

35-
constructor(code: ErrorCode, message: string, details?: AppErrorDetails, cause?: unknown) {
35+
constructor(code: AppErrorCode, message: string, details?: AppErrorDetails, cause?: unknown) {
3636
super(message);
3737
this.code = code;
3838
this.details = details;
@@ -115,7 +115,7 @@ function stripDiagnosticMeta(
115115
return Object.keys(output).length > 0 ? output : undefined;
116116
}
117117

118-
function defaultHintForCode(code: string): string | undefined {
118+
export function defaultHintForCode(code: string): string | undefined {
119119
switch (code) {
120120
case 'INVALID_ARGS':
121121
return 'Check command arguments and run --help for usage examples.';

0 commit comments

Comments
 (0)