Skip to content

Commit

Permalink
fix: show errors with printError()
Browse files Browse the repository at this point in the history
  • Loading branch information
Milly committed Apr 14, 2024
1 parent a290ec4 commit eb3a43b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 79 deletions.
6 changes: 2 additions & 4 deletions denops/ddu/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
UserOptions,
} from "./types.ts";
import { defaultSourceOptions } from "./base/source.ts";
import { printError } from "./utils.ts";

// where
// T: Object
Expand Down Expand Up @@ -357,10 +358,7 @@ export class ContextBuilder {
) {
for (const key in options) {
if (!(key in defaults)) {
await denops.call(
"ddu#util#print_error",
`Invalid ${name}: "${key}"`,
);
await printError(denops, `Invalid ${name}: "${key}"`);
}
}
}
Expand Down
10 changes: 4 additions & 6 deletions denops/ddu/ddu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { Loader } from "./loader.ts";
import {
convertUserString,
errorException,
printError,
treePath2Filename,
} from "./utils.ts";
import {
Expand Down Expand Up @@ -791,8 +792,8 @@ export class Ddu {
}));

if (this.#context.done && this.#options.profile) {
await denops.call(
"ddu#util#print_error",
await printError(
denops,
`Refresh all items: ${Date.now() - this.#startTime} ms`,
);
}
Expand Down Expand Up @@ -942,10 +943,7 @@ export class Ddu {

const action = uiOptions.actions[actionName] ?? ui.actions[actionName];
if (!action) {
await denops.call(
"ddu#util#print_error",
`Not found UI action: ${actionName}`,
);
await printError(denops, `Not found UI action: ${actionName}`);
return;
}

Expand Down
67 changes: 20 additions & 47 deletions denops/ddu/ext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ import { defaultColumnOptions } from "./base/column.ts";
import { defaultKindOptions } from "./base/kind.ts";
import { defaultActionOptions } from "./base/action.ts";
import { Loader } from "./loader.ts";
import { convertUserString, errorException } from "./utils.ts";
import { convertUserString, errorException, printError } from "./utils.ts";

type ItemActions = {
source: BaseSource<BaseSourceParams, unknown>;
Expand Down Expand Up @@ -97,8 +97,8 @@ export async function getItemActions(
];
if (sources.length === 0 || sources.length !== 1 && indexes.length !== 1) {
if (sources.length > 0) {
await denops.call(
"ddu#util#print_error",
await printError(
denops,
`You must not mix multiple sources items: "${
sources.map((source) => source?.name)
}"`,
Expand All @@ -119,10 +119,7 @@ export async function getItemActions(
),
] as string[];
if (kinds.length !== 1) {
await denops.call(
"ddu#util#print_error",
`You must not mix multiple kinds: "${kinds}"`,
);
await printError(denops, `You must not mix multiple kinds: "${kinds}"`);
return null;
}

Expand Down Expand Up @@ -208,8 +205,8 @@ export async function getItemAction(
}

if (actionName === "") {
await denops.call(
"ddu#util#print_error",
await printError(
denops,
`The default action is not defined for the items`,
);
return;
Expand All @@ -230,10 +227,7 @@ export async function getItemAction(
| string
| Action<BaseActionParams>;
if (!action) {
await denops.call(
"ddu#util#print_error",
`Not found action: ${actionName}`,
);
await printError(denops, `Not found action: ${actionName}`);
return;
}

Expand Down Expand Up @@ -320,8 +314,8 @@ export async function getUi(
await loader.autoload(denops, "ui", userUi.name);

if (options.profile) {
await denops.call(
"ddu#util#print_error",
await printError(
denops,
`Load ${userUi.name}: ${Date.now() - startTime} ms`,
);
}
Expand All @@ -330,10 +324,7 @@ export async function getUi(
const ui = loader.getUi(options.name, userUi.name);
if (!ui) {
if (userUi.name.length !== 0) {
await denops.call(
"ddu#util#print_error",
`Not found ui: "${userUi.name}"`,
);
await printError(denops, `Not found ui: "${userUi.name}"`);
}
return [
undefined,
Expand Down Expand Up @@ -367,19 +358,13 @@ export async function getSource(
await loader.autoload(denops, "source", name);

if (options.profile) {
await denops.call(
"ddu#util#print_error",
`Load ${name}: ${Date.now() - startTime} ms`,
);
await printError(denops, `Load ${name}: ${Date.now() - startTime} ms`);
}
}

const source = loader.getSource(options.name, name);
if (!source) {
await denops.call(
"ddu#util#print_error",
`Not found source: ${name}`,
);
await printError(denops, `Not found source: ${name}`);
return [
undefined,
defaultSourceOptions(),
Expand Down Expand Up @@ -416,19 +401,16 @@ export async function getFilter(
await loader.autoload(denops, "filter", userFilter.name);

if (options.profile) {
await denops.call(
"ddu#util#print_error",
await printError(
denops,
`Load ${userFilter.name}: ${Date.now() - startTime} ms`,
);
}
}

const filter = loader.getFilter(options.name, userFilter.name);
if (!filter) {
await denops.call(
"ddu#util#print_error",
`Not found filter: ${userFilter.name}`,
);
await printError(denops, `Not found filter: ${userFilter.name}`);
return [
undefined,
defaultFilterOptions(),
Expand Down Expand Up @@ -460,20 +442,14 @@ async function getKind(
await loader.autoload(denops, "kind", name);

if (options.profile) {
await denops.call(
"ddu#util#print_error",
`Load ${name}: ${Date.now() - startTime} ms`,
);
await printError(denops, `Load ${name}: ${Date.now() - startTime} ms`);
}
}

const kind = loader.getKind(options.name, name);
if (!kind) {
if (name !== "base") {
await denops.call(
"ddu#util#print_error",
`Not found kind: ${name}`,
);
await printError(denops, `Not found kind: ${name}`);
}
return undefined;
}
Expand Down Expand Up @@ -501,19 +477,16 @@ export async function getColumn(
await loader.autoload(denops, "column", userColumn.name);

if (options.profile) {
await denops.call(
"ddu#util#print_error",
await printError(
denops,
`Load ${userColumn.name}: ${Date.now() - startTime} ms`,
);
}
}

const column = loader.getColumn(options.name, userColumn.name);
if (!column) {
await denops.call(
"ddu#util#print_error",
`Not found column: ${userColumn.name}`,
);
await printError(denops, `Not found column: ${userColumn.name}`);
return [
undefined,
defaultColumnOptions(),
Expand Down
40 changes: 18 additions & 22 deletions denops/ddu/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,29 @@ export function treePath2Filename(treePath: TreePath) {
return typeof treePath === "string" ? treePath : treePath.join(pathsep);
}

export async function printError(
denops: Denops,
...messages: unknown[]
) {
const message = messages.map((v) => {
if (v instanceof Error) {
// NOTE: In Deno, Prefer `Error.stack` because it contains `Error.message`.
return `${v.stack ?? v}`;
} else if (typeof v === "object") {
return JSON.stringify(v);
} else {
return `${v}`;
}
}).join("\n");
await denops.call("ddu#util#print_error", message);
}

export async function errorException(
denops: Denops,
e: unknown,
message: string,
) {
await denops.call(
"ddu#util#print_error",
message,
);
if (e instanceof Error) {
await denops.call(
"ddu#util#print_error",
e.message,
);
if (e.stack) {
await denops.call(
"ddu#util#print_error",
e.stack,
);
}
} else {
await denops.call(
"ddu#util#print_error",
"unknown error object",
);
console.error(e);
}
await printError(denops, message, e);
}

export async function safeStat(path: string): Promise<Deno.FileInfo | null> {
Expand Down

0 comments on commit eb3a43b

Please sign in to comment.