Skip to content

Commit

Permalink
feat: show actionable resolutions for native module initialization er…
Browse files Browse the repository at this point in the history
…rors

Show a few common resolutions when the native module fails to initialize for any reason.

Partially resolves #16.
  • Loading branch information
hassankhan committed Jun 8, 2024
1 parent a2f22b1 commit c953c47
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,27 @@ declare global {
var __PrismaProxy: PrismaProxy | undefined;
}

const errorResolutions = [
`1. Ensure at least one migration exists:
$ prisma migrate dev`,

`2. Try removing the node_modules/ folder and running the prebuild command:
$ rm -rf node_modules/
$ expo prebuild --clean
`,
`3. If in a monorepo, ensure the application's package.json also has the required Prisma dependencies:
$ npm i @prisma/client@latest @prisma/react-native@latest react-native-quick-base64
$ yarn add @prisma/client@latest @prisma/react-native@latest react-native-quick-base64
`,
];

const makeErrorMessage = (message: string) => {
return [message, errorResolutions.join('\n\n')].join('\n\n');
};

// @ts-expect-error
const isTurboModuleEnabled = global.__turboModuleProxy != null;

Expand All @@ -21,13 +42,23 @@ const PrismaModule = isTurboModuleEnabled
: NativeModules.Prisma;

if (!PrismaModule) {
throw new Error('🟥 @prisma/react-native failed to initialize');
throw new Error(
makeErrorMessage('🟥 @prisma/react-native failed to initialize')
);
}

PrismaModule.install();
try {
PrismaModule.install();
} catch {
throw new Error(
makeErrorMessage(`🟥 @prisma/react-native failed to initialize`)
);
}

if (!global.__PrismaProxy) {
throw new Error('🟥 prisma/react-native C++ bindings failed to initialize');
throw new Error(
makeErrorMessage('🟥 prisma/react-native C++ bindings failed to initialize')
);
}

// Wrap the create function to stringify the env variables if necessary
Expand Down

0 comments on commit c953c47

Please sign in to comment.