Skip to content

Commit

Permalink
various fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
terrablue committed Aug 4, 2024
1 parent ea69536 commit a2d24f4
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 43 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/private/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});

Expand Down
41 changes: 20 additions & 21 deletions packages/core/src/private/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,35 @@ 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,
info: 2,
};
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");
}
};

Expand All @@ -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) {
Expand All @@ -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);
},
};
4 changes: 1 addition & 3 deletions packages/core/src/serve/hook/handle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion packages/frontend/src/private/error/component-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
});
2 changes: 1 addition & 1 deletion packages/frontend/src/private/error/no-component.js
Original file line number Diff line number Diff line change
@@ -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",
});
36 changes: 22 additions & 14 deletions packages/frontend/src/private/handler.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
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";
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,
});
Expand Down Expand Up @@ -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);
});
};
};
2 changes: 1 addition & 1 deletion packages/store/src/private/error/transaction-rollback.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
});
2 changes: 1 addition & 1 deletion packages/store/src/private/hook/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit a2d24f4

Please sign in to comment.