Skip to content

Commit

Permalink
Use printError()
Browse files Browse the repository at this point in the history
  • Loading branch information
Shougo committed Apr 15, 2024
1 parent efebb1a commit 292babc
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 44 deletions.
9 changes: 4 additions & 5 deletions autoload/dpp/util.vim
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
const s:is_windows = has('win32') || has('win64')

function dpp#util#_error(msg) abort
for mes in a:msg->s:msg2list()
echohl WarningMsg | echomsg '[dpp] ' .. mes | echohl None
echohl Error
for line in a:string->string()->split("\n")
echomsg printf('[%s] %s', a:name, line)
endfor
echohl None
endfunction

function dpp#util#_get_plugins(plugins) abort
Expand Down Expand Up @@ -132,9 +134,6 @@ function dpp#util#_tsort(plugins) abort

return sorted
endfunction
function s:msg2list(expr) abort
return a:expr->type() ==# v:t_list ? a:expr : a:expr->split('\n')
endfunction

function s:tsort_impl(target, mark, sorted) abort
if a:target->empty() || a:mark->has_key(a:target.name)
Expand Down
5 changes: 3 additions & 2 deletions denops/dpp/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
ExtOptions,
ProtocolOptions,
} from "./types.ts";
import { printError } from "./utils.ts";

// where
// T: Object
Expand Down Expand Up @@ -194,8 +195,8 @@ export class ContextBuilder {
) {
for (const key in options) {
if (!(key in defaults)) {
await denops.call(
"dpp#util#_error",
await printError(
denops,
`Invalid ${name}: "${key}"`,
);
}
Expand Down
8 changes: 4 additions & 4 deletions denops/dpp/dpp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ import { ConfigReturn } from "./base/config.ts";
import { extAction, getProtocols } from "./ext.ts";
import {
convert2List,
errorException,
isDirectory,
linkPath,
parseHooksFile,
printError,
} from "./utils.ts";

export class Dpp {
Expand Down Expand Up @@ -186,8 +186,8 @@ export class Dpp {
for (const depend of depends) {
const plugin = recordPlugins[depend];
if (!plugin) {
await denops.call(
"dpp#util#_error",
await printError(
denops,
`Not found dependency: "${depend}"`,
);
continue;
Expand Down Expand Up @@ -405,7 +405,7 @@ export class Dpp {
try {
await denops.cmd(`silent helptags ${docDir}`);
} catch (e: unknown) {
await errorException(
await printError(
denops,
e,
`:helptags failed`,
Expand Down
18 changes: 9 additions & 9 deletions denops/dpp/ext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
import { Loader } from "./loader.ts";
import { defaultExtOptions } from "./base/ext.ts";
import { defaultProtocolOptions } from "./base/protocol.ts";
import { errorException } from "./utils.ts";
import { printError } from "./utils.ts";

export async function getProtocols(
denops: Denops,
Expand Down Expand Up @@ -75,8 +75,8 @@ export async function extAction(

const action = ext.actions[actionName];
if (!action) {
await denops.call(
"dpp#util#_error",
await printError(
denops,
`Not found UI action: ${actionName}`,
);
return;
Expand Down Expand Up @@ -125,8 +125,8 @@ async function getExt(
const ext = loader.getExt(name);
if (!ext) {
if (name.length !== 0) {
await denops.call(
"dpp#util#_error",
await printError(
denops,
`Not found ext: "${name}"`,
);
}
Expand Down Expand Up @@ -162,8 +162,8 @@ async function getProtocol(
const protocol = loader.getProtocol(name);
if (!protocol) {
if (name.length !== 0) {
await denops.call(
"dpp#util#_error",
await printError(
denops,
`Not found protocol: "${name}"`,
);
}
Expand Down Expand Up @@ -204,7 +204,7 @@ async function checkExtOnInit(

ext.isInitialized = true;
} catch (e: unknown) {
await errorException(
await printError(
denops,
e,
`ext: ${ext.name} "onInit()" failed`,
Expand Down Expand Up @@ -253,7 +253,7 @@ async function checkProtocolOnInit(

protocol.isInitialized = true;
} catch (e: unknown) {
await errorException(
await printError(
denops,
e,
`protocol: ${protocol.name} "onInit()" failed`,
Expand Down
36 changes: 12 additions & 24 deletions denops/dpp/utils.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,21 @@
import { assertEquals, Denops, is, join } from "./deps.ts";
import { Plugin } from "./types.ts";

export async function errorException(
export async function printError(
denops: Denops,
e: unknown,
message: string,
...messages: unknown[]
) {
await denops.call(
"dpp#util#_error",
message,
);
if (e instanceof Error) {
await denops.call(
"dpp#util#_error",
e.message,
);
if (e.stack) {
await denops.call(
"dpp#util#_error",
e.stack,
);
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}`;
}
} else {
await denops.call(
"dpp#util#_error",
"unknown error object",
);
console.error(e);
}
}).join("\n");
await denops.call("dpp#util#_error", message);
}

export function convert2List<T>(expr: T | T[] | undefined): T[] {
Expand Down

0 comments on commit 292babc

Please sign in to comment.