@grada-run/grada 0.40.8 → 0.40.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/bin/cli.js CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env node
2
+ import '../src/core/last-resort.js';
2
3
  import path from 'path';
3
4
  import { mainStack } from '../src/commands/init.js';
4
5
  import { destroyStack, parseDestroyArgs } from '../src/commands/destroy.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@grada-run/grada",
3
- "version": "0.40.8",
3
+ "version": "0.40.9",
4
4
  "description": "Provision production-ready AWS infrastructure and CI/CD pipelines in seconds.",
5
5
  "engines": {
6
6
  "node": ">=18.0.0"
@@ -0,0 +1,119 @@
1
+ import fs from 'node:fs';
2
+
3
+ export const ISSUES_URL = 'https://github.com/grada-run/grada/issues';
4
+
5
+ // Last-resort crash backstop: unhandled rejections (e.g. the SDK
6
+ // credential chain exploding without a HOME) and uncaught exceptions
7
+ // (e.g. a future import-time throw) render one formatted report and
8
+ // exit 1 instead of dumping a raw stack — or printing nothing at all
9
+ // in --json mode. Registered via a side-effect import as the first
10
+ // line of bin/cli.js so it precedes every other listener (clack
11
+ // spinners register their own print-only rejection listener at
12
+ // start(); exiting first keeps their banner from double-surfacing).
13
+ //
14
+ // Dependency-free by design (node:* only): the crash backstop must
15
+ // never import the application modules that might be mid-failure.
16
+ // Output rule: a known environmental cause (the smithy no-HOME
17
+ // shape) prints guidance without a stack; everything else prints a
18
+ // one-liner, the stack (or String(reason) when there is none), and
19
+ // the report pointer. JSON mode puts the envelope on stdout and the
20
+ // human text on stderr so parsers stay safe.
21
+
22
+ function firstLine(text) {
23
+ return String(text).split('\n')[0];
24
+ }
25
+
26
+ function reasonMessage(reason) {
27
+ if (reason instanceof Error) return reason.message || String(reason);
28
+ return String(reason);
29
+ }
30
+
31
+ function reasonDetail(reason) {
32
+ if (reason instanceof Error && typeof reason.stack === 'string') return reason.stack;
33
+ return String(reason);
34
+ }
35
+
36
+ // Matches smithy's shared-config loader failing on an undeterminable
37
+ // home directory (both observed uv variants). Deliberately tight:
38
+ // near-misses fail open to the stack-bearing path below.
39
+ function isSmithyHomelessRejection(reason) {
40
+ if (!reason || typeof reason !== 'object') return false;
41
+ const codeMatch = reason.code === 'ERR_SYSTEM_ERROR' || reason.code === 'ENOENT';
42
+ if (!codeMatch) return false;
43
+ return /homedir|passwd/i.test(`${reason.syscall || ''} ${reason.message || ''}`);
44
+ }
45
+
46
+ const HOMELESS_GUIDANCE = 'AWS credentials unavailable: the home directory is undeterminable, so credential files cannot be loaded.';
47
+
48
+ export function formatLastResortOutput({ kind, reason, isJson }) {
49
+ const first = firstLine(reasonMessage(reason));
50
+ const detail = reasonDetail(reason);
51
+ let oneLiner;
52
+ let stderrText;
53
+ if (kind === 'unhandledRejection' && isSmithyHomelessRejection(reason)) {
54
+ oneLiner = HOMELESS_GUIDANCE;
55
+ stderrText = `✖ ${HOMELESS_GUIDANCE}\n`
56
+ + ' Set the HOME environment variable, or provide credentials via AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY.\n';
57
+ } else if (kind === 'uncaughtException') {
58
+ oneLiner = `Grada crashed unexpectedly: ${first}`;
59
+ stderrText = `✖ ${oneLiner}\n${detail}\n Please report this: ${ISSUES_URL}\n`;
60
+ } else {
61
+ oneLiner = `Unexpected failure: ${first}`;
62
+ stderrText = `✖ ${oneLiner}\n${detail}\n Please report this: ${ISSUES_URL}\n`;
63
+ }
64
+ if (!isJson) return { stdoutText: null, stderrText };
65
+ const envelope = {
66
+ ok: false,
67
+ exitCode: 1,
68
+ error: oneLiner,
69
+ reason: kind === 'uncaughtException' ? 'uncaught-exception' : 'unhandled-rejection',
70
+ };
71
+ return { stdoutText: `${JSON.stringify(envelope)}\n`, stderrText };
72
+ }
73
+
74
+ export function createLastResortHandlers({
75
+ argv = process.argv,
76
+ exitFn = (code) => process.exit(code),
77
+ writeStdout = (text) => fs.writeSync(1, text),
78
+ writeStderr = (text) => fs.writeSync(2, text),
79
+ isTestEnv = () => Boolean(process.env.VITEST),
80
+ } = {}) {
81
+ const isJson = argv.includes('--json');
82
+ const handle = (kind) => (reason) => {
83
+ // Test workers keep native rejection behavior: a process.exit
84
+ // here would kill the worker mid-suite.
85
+ if (isTestEnv()) return;
86
+ try {
87
+ const { stdoutText, stderrText } = formatLastResortOutput({ kind, reason, isJson });
88
+ if (stdoutText !== null) {
89
+ try {
90
+ writeStdout(stdoutText);
91
+ } catch {
92
+ // A broken stdout must not swallow the stderr report.
93
+ }
94
+ }
95
+ writeStderr(stderrText);
96
+ } catch {
97
+ try {
98
+ writeStderr('✖ Grada crashed unexpectedly.\n');
99
+ } catch {
100
+ // A crash handler that crashes is the worst outcome;
101
+ // fall through to the exit below no matter what.
102
+ }
103
+ }
104
+ exitFn(1);
105
+ };
106
+ return {
107
+ unhandledRejection: handle('unhandledRejection'),
108
+ uncaughtException: handle('uncaughtException'),
109
+ };
110
+ }
111
+
112
+ export function registerLastResortHandlers(options = {}) {
113
+ const handlers = createLastResortHandlers(options);
114
+ process.on('unhandledRejection', handlers.unhandledRejection);
115
+ process.on('uncaughtException', handlers.uncaughtException);
116
+ return handlers;
117
+ }
118
+
119
+ registerLastResortHandlers();
@@ -357,7 +357,7 @@ function telemetryPreference() {
357
357
  export function writeTelemetryPreference(enabled, { configPath } = {}) {
358
358
  const target = configPath ?? resolveConfigPath();
359
359
  if (target === null) {
360
- throw new Error('Cannot save the telemetry preference: the home directory is undeterminable (HOME is unset and no passwd entry exists).');
360
+ throw new Error('the home directory is undeterminable (HOME is unset and no passwd entry exists).');
361
361
  }
362
362
  let current = {};
363
363
  try {