Skip to content

Commit 2d7e794

Browse files
committed
fix: improve error handling
1 parent 28747a2 commit 2d7e794

File tree

6 files changed

+29
-23
lines changed

6 files changed

+29
-23
lines changed

packages/react-native-builder-bob/src/targets/codegen/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { patchCodegenAndroidPackage } from './patches/patchCodegenAndroidPackage
44
import fs from 'fs-extra';
55
import path from 'path';
66
import del from 'del';
7-
import { runRNCCli } from '../../utils/runRNCCli';
87
import { removeCodegenAppLevelCode } from './patches/removeCodegenAppLevelCode';
8+
import { spawn } from '../../utils/spawn';
99

1010
type Options = Omit<Input, 'output'>;
1111

@@ -42,7 +42,7 @@ export default async function build({ root, report }: Options) {
4242
}
4343

4444
try {
45-
await runRNCCli(['codegen']);
45+
await spawn('npx', ['@react-native-community/cli', 'codegen']);
4646

4747
if (codegenType === 'modules' || codegenType === 'all') {
4848
await patchCodegenAndroidPackage(root, packageJson, report);
@@ -53,22 +53,22 @@ export default async function build({ root, report }: Options) {
5353
} catch (e: unknown) {
5454
if (e != null && typeof e === 'object') {
5555
if ('stdout' in e && e.stdout != null) {
56-
throw new Error(
57-
`Errors found while generating codegen files:\n${e.stdout.toString()}`
56+
report.error(
57+
`Errors found while running codegen:\n\n${e.stdout.toString()}`
5858
);
5959
} else if ('message' in e && typeof e.message === 'string') {
6060
if (
6161
e.message.includes(
6262
"Error: Cannot find module '@react-native-community/cli/package.json'"
6363
)
6464
) {
65-
throw new Error(
65+
report.error(
6666
"You don't have `@react-native-community/cli` in your root package's dev dependencies. Please install it and make sure it uses the same version as your application."
6767
);
6868
}
6969
}
7070
}
7171

72-
throw e;
72+
throw new Error('Failed to run codegen.', { cause: e });
7373
}
7474
}

packages/react-native-builder-bob/src/targets/typescript.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,6 @@ export default async function build({
424424
throw e;
425425
}
426426

427-
throw new Error('Failed to build definition files.');
427+
throw new Error('Failed to build definition files.', { cause: e });
428428
}
429429
}

packages/react-native-builder-bob/src/utils/androidAssemble.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export default async function androidAssemble({
2525
);
2626

2727
const gradleWrapper = platform() === 'win32' ? 'gradlew.bat' : './gradlew';
28+
2829
if (await fs.pathExists(path.join(androidPath, gradleWrapper))) {
2930
execFileSync(gradleWrapper, ['assemble'], { cwd: androidPath });
3031
} else {

packages/react-native-builder-bob/src/utils/compile.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,9 @@ export default async function compile({
307307
}`
308308
);
309309

310-
throw new Error(`Found incorrect path in '${name}' field.`);
310+
throw new Error(`Found incorrect path in '${name}' field.`, {
311+
cause: e,
312+
});
311313
}
312314

313315
throw e;

packages/react-native-builder-bob/src/utils/runRNCCli.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

packages/react-native-builder-bob/src/utils/spawn.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,24 @@ export const spawn = async (...args: Parameters<typeof crossSpawn>) => {
2222
if (code === 0) {
2323
resolve(stdout.trim());
2424
} else {
25-
reject(new Error(stderr.trim()));
25+
const error = new Error(stderr.trim() || 'Unknown error');
26+
27+
Object.defineProperties(error, {
28+
stdout: {
29+
enumerable: false,
30+
value: stdout,
31+
},
32+
stderr: {
33+
enumerable: false,
34+
value: stderr,
35+
},
36+
code: {
37+
enumerable: false,
38+
value: code,
39+
},
40+
});
41+
42+
reject(error);
2643
}
2744
});
2845
});

0 commit comments

Comments
 (0)