Skip to content

Commit

Permalink
feat: add filter option
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Nov 21, 2023
1 parent 8cb864d commit 0c53af1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
8 changes: 8 additions & 0 deletions packages/app/src/components/stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,14 @@ export function StoryRuntimeErrorOverlay() {
>
unhandledrejection
</button>
<button
className="flex-1 antd-btn antd-btn-default"
onClick={() => {
throw new Error("test error (filter out)");
}}
>
error (filter-out)
</button>
</div>
</section>
</main>
Expand Down
4 changes: 3 additions & 1 deletion packages/app/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ export default defineConfig({
unocss(),
unocssDepHmrPlugin([require.resolve("@hiogawa/unocss-preset-antd")]),
vitePluginTinyRefresh(),
viteRuntimeErrorOverlayPlugin(),
viteRuntimeErrorOverlayPlugin({
filter: (error) => !error.message.includes("(filter out)"),
}),
themeScriptPlugin({
storageKey: "unocss-preset-antd-app:theme",
}),
Expand Down
10 changes: 8 additions & 2 deletions packages/vite-runtime-error-overlay/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import { name as packageName } from "../package.json";
// generateErrorPayload
// generateFrame

export function viteRuntimeErrorOverlayPlugin(): Plugin {
export function viteRuntimeErrorOverlayPlugin(options?: {
filter?: (error: Error) => boolean;
}): Plugin {
return {
name: packageName,

Expand All @@ -33,6 +35,10 @@ export function viteRuntimeErrorOverlayPlugin(): Plugin {
// deserialize error
const error = Object.assign(new Error(), data);

if (options?.filter && !options.filter(error)) {
return;
}

// https://vitejs.dev/guide/api-plugin.html#client-server-communication
// https://github.com/vitejs/vite/blob/5b58eca05939c0667cf9698e83f4f4849f3296f4/packages/vite/src/node/server/middlewares/error.ts#L54-L57
client.send({
Expand All @@ -54,7 +60,7 @@ const CLIENT_SCRIPT = /* js */ `
import { createHotContext } from "/@vite/client";
// dummy file path to instantiate import.meta.hot
const hot = createHotContext("/__runtimeErrorOverlayPlugin__.js");
const hot = createHotContext("/__dummy__${packageName}");
function sendError(error) {
if (!(error instanceof Error)) {
Expand Down

0 comments on commit 0c53af1

Please sign in to comment.