From a2d24f4ed663c553091c233237f9deed5c6d15c3 Mon Sep 17 00:00:00 2001 From: terrablue <102580937+terrablue@users.noreply.github.com> Date: Sun, 4 Aug 2024 23:47:28 +0200 Subject: [PATCH] various fixes --- packages/core/src/private/error.js | 2 +- packages/core/src/private/log.js | 41 +++++++++---------- packages/core/src/serve/hook/handle.js | 4 +- .../src/private/error/component-error.js | 2 +- .../src/private/error/no-component.js | 2 +- packages/frontend/src/private/handler.js | 36 +++++++++------- .../src/private/error/transaction-rollback.js | 2 +- packages/store/src/private/hook/route.js | 2 +- 8 files changed, 48 insertions(+), 43 deletions(-) diff --git a/packages/core/src/private/error.js b/packages/core/src/private/error.js index 902f247a..11b57f65 100644 --- a/packages/core/src/private/error.js +++ b/packages/core/src/private/error.js @@ -4,7 +4,7 @@ import file from "@rcompat/fs/file"; const base = level => (url, args) => (...params) => log[level]({ params, name: file(url).base, - module: "primate", + module: "@primate/core", ...args, }); diff --git a/packages/core/src/private/log.js b/packages/core/src/private/log.js index 914a5de9..8ca4ee91 100644 --- a/packages/core/src/private/log.js +++ b/packages/core/src/private/log.js @@ -7,10 +7,10 @@ import red from "@rcompat/cli/color/red"; import yellow from "@rcompat/cli/color/yellow"; import print from "@rcompat/cli/print"; -const reference = (module, error) => { - const base = module === "primate" ? "guide/logging" : `modules/${module}`; - return `https://primatejs.com/${base}#${error}`; -}; +const url = "https://primatejs.com/errors"; +const slice_length = "@primate/".length; +const helpat = (name, error) => `${url}/${name.slice(slice_length)}#${error}`; + const levels = { error: 0, warn: 1, @@ -18,25 +18,24 @@ const levels = { }; const level = levels[loglevel]; -const make_error = (level, { message, fix, name, params, module }) => { - const error = new Error(mark(message, ...params)); - error.level = level; - error.fix = mark(fix, ...params); - error.name = name; - error.module = module; - return error; -}; +const make_error = (level, { message, fix, name, params, module }) => ({ + level, + fix: mark(fix, ...params), + message: mark(message, ...params), + name, + module, +}); const normalize = (level, message) => typeof message === "string" ? { message } : make_error(level, message); -const log = (pre, color, error) => { - const { fix, module, name, message } = error; +const log = (pre, color, error, override) => { + const { fix, module, name, message } = { ...error, ...override }; print(color(pre), `${module !== undefined ? `${color(module)} ` : ""}${message}`, "\n"); if (fix) { print(blue("++"), fix); - name && print(dim(`\n -> ${reference(module, name)}`), "\n"); + name && print(dim(`\n -> ${helpat(module, name)}`), "\n"); } }; @@ -45,17 +44,17 @@ export default { log("++", blue, { message }); }, - info(error) { + info(error, override) { // info prints only on info level - level === levels.info && log("--", green, normalize("info", error)); + level === levels.info && log("--", green, normalize("info", error), override); }, - warn(error) { + warn(error, override) { // warn prints on info and warn levels - level >= levels.warn && log("??", yellow, normalize("warn", error)); + level >= levels.warn && log("??", yellow, normalize("warn", error), override); }, - error(error, toss = true) { + error(error, override, toss = true) { // error always prints log("!!", red, normalize("error", error)); if (toss) { @@ -65,6 +64,6 @@ export default { }, auto(error) { - Object.keys(levels).includes(error.level) && this[error.level](error, false); + Object.keys(levels).includes(error.level) && this[error.level](error, {}, false); }, }; diff --git a/packages/core/src/serve/hook/handle.js b/packages/core/src/serve/hook/handle.js index f8060506..169392b4 100644 --- a/packages/core/src/serve/hook/handle.js +++ b/packages/core/src/serve/hook/handle.js @@ -64,9 +64,7 @@ export default app => { const $layouts = { layouts: await get_layouts(layouts, routed.request) }; return respond(routed.response)(app, $layouts, routed.request); - }).orelse(async error => { - log.auto(error); - + }).orelse(async () => { // the +error.js page itself could fail return tryreturn(_ => respond(error_handler(request))(app, {}, request)) .orelse(_ => client_error()(app, {}, request)); diff --git a/packages/frontend/src/private/error/component-error.js b/packages/frontend/src/private/error/component-error.js index c6dbfaed..73566ec1 100644 --- a/packages/frontend/src/private/error/component-error.js +++ b/packages/frontend/src/private/error/component-error.js @@ -2,5 +2,5 @@ import { error } from "#error"; export default error(import.meta.url, { message: "error in component {0}", - fix: "fix previous error in {1}\n{2}", + fix: "fix component error: {1}", }); diff --git a/packages/frontend/src/private/error/no-component.js b/packages/frontend/src/private/error/no-component.js index 2d3a2dc5..82943ee7 100644 --- a/packages/frontend/src/private/error/no-component.js +++ b/packages/frontend/src/private/error/no-component.js @@ -1,6 +1,6 @@ import { error } from "#error"; -export default error({ +export default error(import.meta.url, { message: "missing component {0}", fix: "create {1} or remove route function", }); diff --git a/packages/frontend/src/private/handler.js b/packages/frontend/src/private/handler.js index 0b0ab816..ba676d83 100644 --- a/packages/frontend/src/private/handler.js +++ b/packages/frontend/src/private/handler.js @@ -1,3 +1,5 @@ +import component_error from "#error/component-error"; +import no_component from "#error/no-component"; import normalize from "#normalize"; import cascade from "@rcompat/async/cascade"; import map from "@rcompat/async/map"; @@ -5,12 +7,15 @@ import { json } from "@rcompat/http/mime"; import { OK } from "@rcompat/http/status"; import filter from "@rcompat/object/filter"; import valmap from "@rcompat/object/valmap"; +import tryreturn from "@rcompat/async/tryreturn"; const register = ({ app, name: rootname, ...rest }) => ({ root: app.get_component(`root_${rootname}.js`), async load(name, props) { const component = await app.get_component(name); - return { name, props, component }; + return component === undefined + ? no_component(name, `${app.get("location.components")}/${name}`) + : { name, props, component }; }, ...rest, }); @@ -57,20 +62,23 @@ export default config => { }); } - const { body, head } = render(root, { - components: components.map(({ component }) => component), - ...shared, - }); - - const code = client({ names, ...shared }, { spa: config.spa }); - const inlined = await app.inline(code, "module"); - const script_src = [inlined.integrity]; + return tryreturn(async () => { + const { body, head } = render(root, { + components: components.map(({ component }) => component), + ...shared, + }); + const code = client({ names, ...shared }, { spa: config.spa }); + const inlined = await app.inline(code, "module"); + const script_src = [inlined.integrity]; - return app.view({ - body, - head: head.concat(inlined.head), - headers: app.headers({ "script-src": script_src }), - ...options, + return app.view({ + body, + head: head.concat(inlined.head), + headers: app.headers({ "script-src": script_src }), + ...options, + }); + }).orelse(error => { + component_error(`${app.get("location.components")}/${name}`, error); }); }; }; diff --git a/packages/store/src/private/error/transaction-rollback.js b/packages/store/src/private/error/transaction-rollback.js index 3a91d94e..2dc96443 100644 --- a/packages/store/src/private/error/transaction-rollback.js +++ b/packages/store/src/private/error/transaction-rollback.js @@ -2,5 +2,5 @@ import { warn } from "#error"; export default warn(import.meta.url, { message: "transaction {0} rolled back due to previous error", - fix: "address previous {1} error", + fix: "fix previous error: {1}", }); diff --git a/packages/store/src/private/hook/route.js b/packages/store/src/private/hook/route.js index aacc5240..6d41bf02 100644 --- a/packages/store/src/private/hook/route.js +++ b/packages/store/src/private/hook/route.js @@ -32,7 +32,7 @@ export default env => async (request, next) => { ); } catch (error) { log.auto(error); - transaction_rollback(id, error.name); + transaction_rollback(id, error); // let core handle error throw error;