@aerogel/core 0.0.0-next.7f6ed5a1f91688a86bf5ede2adc465e4fd6cfdea → 0.0.0-next.b58141fee5d2fe7d25debdbca6b1d2bf1c13e48e

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.
Files changed (85) hide show
  1. package/dist/aerogel-core.cjs.js +1 -1
  2. package/dist/aerogel-core.cjs.js.map +1 -1
  3. package/dist/aerogel-core.d.ts +711 -79
  4. package/dist/aerogel-core.esm.js +1 -1
  5. package/dist/aerogel-core.esm.js.map +1 -1
  6. package/dist/virtual.d.ts +11 -0
  7. package/noeldemartin.config.js +4 -1
  8. package/package.json +7 -9
  9. package/src/bootstrap/bootstrap.test.ts +0 -56
  10. package/src/bootstrap/index.ts +9 -25
  11. package/src/bootstrap/options.ts +5 -1
  12. package/src/components/AGAppModals.vue +15 -0
  13. package/src/components/AGAppOverlays.vue +5 -7
  14. package/src/components/AGAppSnackbars.vue +13 -0
  15. package/src/components/basic/AGErrorMessage.vue +16 -0
  16. package/src/components/basic/AGLink.vue +9 -0
  17. package/src/components/basic/AGMarkdown.vue +21 -5
  18. package/src/components/basic/index.ts +3 -1
  19. package/src/components/constants.ts +8 -0
  20. package/src/components/forms/AGButton.vue +36 -3
  21. package/src/components/forms/AGCheckbox.vue +35 -0
  22. package/src/components/forms/AGInput.vue +8 -4
  23. package/src/components/forms/index.ts +2 -1
  24. package/src/components/headless/forms/AGHeadlessButton.vue +7 -7
  25. package/src/components/headless/forms/AGHeadlessInput.ts +2 -2
  26. package/src/components/headless/forms/AGHeadlessInput.vue +5 -5
  27. package/src/components/headless/forms/AGHeadlessInputError.vue +9 -5
  28. package/src/components/headless/forms/AGHeadlessInputInput.vue +20 -4
  29. package/src/components/headless/forms/AGHeadlessInputLabel.vue +16 -0
  30. package/src/components/headless/forms/index.ts +6 -4
  31. package/src/components/headless/index.ts +1 -0
  32. package/src/components/headless/modals/AGHeadlessModal.vue +5 -1
  33. package/src/components/headless/modals/AGHeadlessModalPanel.vue +10 -2
  34. package/src/components/headless/snackbars/AGHeadlessSnackbar.vue +10 -0
  35. package/src/components/headless/snackbars/index.ts +25 -0
  36. package/src/components/index.ts +4 -1
  37. package/src/components/modals/AGAlertModal.vue +12 -2
  38. package/src/components/modals/AGConfirmModal.vue +30 -0
  39. package/src/components/modals/AGErrorReportModal.ts +20 -0
  40. package/src/components/modals/AGErrorReportModal.vue +62 -0
  41. package/src/components/modals/AGErrorReportModalButtons.vue +109 -0
  42. package/src/components/modals/AGErrorReportModalTitle.vue +25 -0
  43. package/src/components/modals/AGLoadingModal.vue +19 -0
  44. package/src/components/modals/AGModal.ts +4 -0
  45. package/src/components/modals/AGModal.vue +23 -4
  46. package/src/components/modals/AGModalTitle.vue +9 -0
  47. package/src/components/modals/index.ts +20 -2
  48. package/src/components/snackbars/AGSnackbar.vue +42 -0
  49. package/src/components/snackbars/index.ts +3 -0
  50. package/src/directives/index.ts +19 -4
  51. package/src/errors/Errors.state.ts +31 -0
  52. package/src/errors/Errors.ts +183 -0
  53. package/src/errors/index.ts +59 -0
  54. package/src/forms/Form.test.ts +21 -0
  55. package/src/forms/Form.ts +38 -16
  56. package/src/forms/utils.ts +17 -0
  57. package/src/lang/Lang.ts +47 -8
  58. package/src/lang/index.ts +17 -76
  59. package/src/lang/utils.ts +4 -0
  60. package/src/main.ts +2 -0
  61. package/src/plugins/Plugin.ts +7 -0
  62. package/src/plugins/index.ts +7 -0
  63. package/src/services/App.state.ts +16 -0
  64. package/src/services/App.ts +15 -0
  65. package/src/services/Service.ts +157 -29
  66. package/src/services/index.ts +32 -7
  67. package/src/services/store.ts +27 -0
  68. package/src/types/virtual.d.ts +11 -0
  69. package/src/types/vite.d.ts +0 -2
  70. package/src/ui/UI.state.ts +12 -6
  71. package/src/ui/UI.ts +72 -10
  72. package/src/ui/index.ts +24 -14
  73. package/src/utils/composition/forms.ts +11 -0
  74. package/src/utils/composition/hooks.ts +9 -0
  75. package/src/utils/index.ts +3 -0
  76. package/src/utils/markdown.ts +11 -2
  77. package/src/utils/vue.ts +2 -0
  78. package/tsconfig.json +2 -10
  79. package/vite.config.ts +3 -6
  80. package/src/bootstrap/hooks.ts +0 -19
  81. package/src/lang/helpers.ts +0 -5
  82. package/src/models/index.ts +0 -18
  83. package/src/routing/index.ts +0 -33
  84. package/src/testing/stubs/lang/en.yaml +0 -1
  85. package/src/testing/stubs/models/User.ts +0 -3
@@ -0,0 +1,62 @@
1
+ <template>
2
+ <AGModal>
3
+ <div>
4
+ <h2 class="flex items-center justify-between text-lg font-medium">
5
+ <div class="flex items-center">
6
+ <AGErrorReportModalTitle
7
+ :report="report"
8
+ :current-report="activeReportIndex + 1"
9
+ :total-reports="reports.length"
10
+ />
11
+ <template v-if="reports.length > 1">
12
+ <AGButton
13
+ color="clear"
14
+ :disabled="activeReportIndex === 0"
15
+ :title="$td('errors.previousReport', 'Show previous report')"
16
+ :aria-label="$td('errors.previousReport', 'Show previous report')"
17
+ @click="activeReportIndex--"
18
+ >
19
+ <IconCheveronLeft aria-hidden="true" class="h-4 w-4" />
20
+ </AGButton>
21
+ <AGButton
22
+ color="clear"
23
+ :disabled="activeReportIndex === reports.length - 1"
24
+ :title="$td('errors.nextReport', 'Show next report')"
25
+ :aria-label="$td('errors.nextReport', 'Show next report')"
26
+ @click="activeReportIndex++"
27
+ >
28
+ <IconCheveronRight aria-hidden="true" class="h-4 w-4" />
29
+ </AGButton>
30
+ </template>
31
+ </div>
32
+ <AGErrorReportModalButtons :report="report" />
33
+ </h2>
34
+ <AGMarkdown v-if="report.description" :text="report.description" class="mt-2" />
35
+ </div>
36
+ <pre
37
+ class="h-full overflow-auto bg-gray-200 p-4 text-xs text-red-900"
38
+ v-text="report.details ?? $td('errors.detailsEmpty', 'This error is missing a stacktrace.')"
39
+ />
40
+ </AGModal>
41
+ </template>
42
+
43
+ <script setup lang="ts">
44
+ import IconCheveronRight from '~icons/zondicons/cheveron-right';
45
+ import IconCheveronLeft from '~icons/zondicons/cheveron-left';
46
+
47
+ import { computed, ref } from 'vue';
48
+
49
+ import type { ErrorReport } from '@/errors';
50
+
51
+ import { useErrorReportModalProps } from './AGErrorReportModal';
52
+
53
+ import AGButton from '../forms/AGButton.vue';
54
+ import AGErrorReportModalButtons from './AGErrorReportModalButtons.vue';
55
+ import AGErrorReportModalTitle from './AGErrorReportModalTitle.vue';
56
+ import AGMarkdown from '../basic/AGMarkdown.vue';
57
+ import AGModal from './AGModal.vue';
58
+
59
+ const props = defineProps(useErrorReportModalProps());
60
+ const activeReportIndex = ref(0);
61
+ const report = computed(() => props.reports[activeReportIndex.value] as ErrorReport);
62
+ </script>
@@ -0,0 +1,109 @@
1
+ <template>
2
+ <div class="flex">
3
+ <slot v-for="(button, i) of buttons" v-bind="(button as unknown as ComponentProps)" :key="i">
4
+ <AGButton
5
+ color="clear"
6
+ :url="button.url"
7
+ :title="$td(`errors.report_${button.id}`, button.description)"
8
+ :aria-label="$td(`errors.report_${button.id}`, button.description)"
9
+ @click="button.handler"
10
+ >
11
+ <component :is="button.iconComponent" class="h-4 w-4" aria-hidden="true" />
12
+ </AGButton>
13
+ </slot>
14
+ </div>
15
+ </template>
16
+
17
+ <script setup lang="ts">
18
+ import IconConsole from '~icons/mdi/console';
19
+ import IconCopy from '~icons/zondicons/copy';
20
+ import IconGitHub from '~icons/mdi/github';
21
+
22
+ import { computed } from 'vue';
23
+ import { stringExcerpt, tap } from '@noeldemartin/utils';
24
+
25
+ import App from '@/services/App';
26
+ import UI from '@/ui/UI';
27
+ import { requiredObjectProp } from '@/utils/vue';
28
+ import { translateWithDefault } from '@/lang/utils';
29
+ import type { ComponentProps } from '@/utils/vue';
30
+ import type { ErrorReport } from '@/errors';
31
+
32
+ import AGButton from '../forms/AGButton.vue';
33
+ import type { IAGErrorReportModalButtonsDefaultSlotProps } from './AGErrorReportModal';
34
+
35
+ const props = defineProps({
36
+ report: requiredObjectProp<ErrorReport>(),
37
+ });
38
+ const summary = computed(() =>
39
+ props.report.description ? `${props.report.title}: ${props.report.description}` : props.report.title);
40
+ const githubReportUrl = computed(() => {
41
+ if (!App.sourceUrl) {
42
+ return false;
43
+ }
44
+
45
+ const issueTitle = encodeURIComponent(summary.value);
46
+ const issueBody = encodeURIComponent(
47
+ [
48
+ '[Please, explain here what you were trying to do when this error appeared]',
49
+ '',
50
+ 'Error details:',
51
+ '```',
52
+ stringExcerpt(
53
+ props.report.details ?? 'Details missing from report',
54
+ 1800 - issueTitle.length - App.sourceUrl.length,
55
+ ).trim(),
56
+ '```',
57
+ ].join('\n'),
58
+ );
59
+
60
+ return `${App.sourceUrl}/issues/new?title=${issueTitle}&body=${issueBody}`;
61
+ });
62
+ const buttons = computed(() =>
63
+ tap(
64
+ [
65
+ {
66
+ id: 'clipboard',
67
+ description: 'Copy to clipboard',
68
+ iconComponent: IconCopy,
69
+ async handler() {
70
+ await navigator.clipboard.writeText(`${summary.value}\n\n${props.report.details}`);
71
+
72
+ UI.showSnackbar(
73
+ translateWithDefault('errors.copiedToClipboard', 'Debug information copied to clipboard'),
74
+ );
75
+ },
76
+ },
77
+ {
78
+ id: 'console',
79
+ description: 'Log to console',
80
+ iconComponent: IconConsole,
81
+ handler() {
82
+ (window as { error?: unknown }).error = props.report.error;
83
+
84
+ // eslint-disable-next-line no-console
85
+ console.error(props.report.error);
86
+
87
+ UI.showSnackbar(
88
+ translateWithDefault(
89
+ 'errors.addedToConsole',
90
+ 'You can now use the **error** variable in the console',
91
+ ),
92
+ );
93
+ },
94
+ },
95
+ ],
96
+ (reportButtons: IAGErrorReportModalButtonsDefaultSlotProps[]) => {
97
+ if (!githubReportUrl.value) {
98
+ return;
99
+ }
100
+
101
+ reportButtons.push({
102
+ id: 'github',
103
+ description: 'Report in GitHub',
104
+ iconComponent: IconGitHub,
105
+ url: githubReportUrl.value,
106
+ });
107
+ },
108
+ ));
109
+ </script>
@@ -0,0 +1,25 @@
1
+ <template>
2
+ <AGMarkdown :text="text" inline />
3
+ </template>
4
+
5
+ <script setup lang="ts">
6
+ import { computed } from 'vue';
7
+
8
+ import { numberProp, requiredObjectProp } from '@/utils/vue';
9
+ import type { ErrorReport } from '@/errors';
10
+
11
+ import AGMarkdown from '../basic/AGMarkdown.vue';
12
+
13
+ const props = defineProps({
14
+ report: requiredObjectProp<ErrorReport>(),
15
+ currentReport: numberProp(),
16
+ totalReports: numberProp(),
17
+ });
18
+ const text = computed(() => {
19
+ if (!props.totalReports || props.totalReports <= 1) {
20
+ return props.report.title;
21
+ }
22
+
23
+ return `${props.report.title} (${props.currentReport}/${props.totalReports})`;
24
+ });
25
+ </script>
@@ -0,0 +1,19 @@
1
+ <template>
2
+ <AGModal :cancellable="false">
3
+ <AGMarkdown :text="renderedMessage" />
4
+ </AGModal>
5
+ </template>
6
+
7
+ <script setup lang="ts">
8
+ import { computed } from 'vue';
9
+
10
+ import { stringProp } from '@/utils/vue';
11
+ import { translateWithDefault } from '@/lang/utils';
12
+
13
+ import AGModal from './AGModal.vue';
14
+
15
+ import AGMarkdown from '../basic/AGMarkdown.vue';
16
+
17
+ const props = defineProps({ message: stringProp() });
18
+ const renderedMessage = computed(() => props.message ?? translateWithDefault('ui.loading', 'Loading...'));
19
+ </script>
@@ -4,3 +4,7 @@ export interface IAGModal {
4
4
  cancellable: Ref<boolean>;
5
5
  close(): Promise<void>;
6
6
  }
7
+
8
+ export interface IAGModalDefaultSlotProps {
9
+ close(result?: unknown): Promise<void>;
10
+ }
@@ -1,8 +1,13 @@
1
1
  <template>
2
- <AGHeadlessModal v-slot="{ close }: IAGHeadlessModalDefaultSlotProps" class="relative z-50">
3
- <div class="fixed inset-0 flex items-center justify-center">
2
+ <AGHeadlessModal
3
+ ref="$headlessModal"
4
+ v-slot="{ close }: IAGHeadlessModalDefaultSlotProps"
5
+ :cancellable="cancellable"
6
+ class="relative z-50"
7
+ >
8
+ <div class="fixed inset-0 flex items-center justify-center p-8">
4
9
  <AGHeadlessModalPanel class="flex max-h-full max-w-full flex-col overflow-hidden bg-white">
5
- <div class="flex max-h-full flex-col overflow-auto p-4">
10
+ <div class="flex max-h-full flex-col overflow-auto p-4" v-bind="$attrs">
6
11
  <slot :close="close" />
7
12
  </div>
8
13
  </AGHeadlessModalPanel>
@@ -11,8 +16,22 @@
11
16
  </template>
12
17
 
13
18
  <script setup lang="ts">
14
- import type { IAGHeadlessModalDefaultSlotProps } from '@/components/headless/modals/AGHeadlessModal';
19
+ import { computed, ref } from 'vue';
20
+
21
+ import { booleanProp } from '@/utils';
22
+ import type { IAGHeadlessModal, IAGHeadlessModalDefaultSlotProps } from '@/components/headless/modals/AGHeadlessModal';
23
+
24
+ import type { IAGModal } from './AGModal';
15
25
 
16
26
  import AGHeadlessModal from '../headless/modals/AGHeadlessModal.vue';
17
27
  import AGHeadlessModalPanel from '../headless/modals/AGHeadlessModalPanel.vue';
28
+
29
+ const $headlessModal = ref<IAGHeadlessModal>();
30
+
31
+ defineOptions({ inheritAttrs: false });
32
+ defineProps({ cancellable: booleanProp(true) });
33
+ defineExpose<IAGModal>({
34
+ close: async () => $headlessModal.value?.close(),
35
+ cancellable: computed(() => !!$headlessModal.value?.cancellable),
36
+ });
18
37
  </script>
@@ -0,0 +1,9 @@
1
+ <template>
2
+ <AGHeadlessModalTitle class="mb-2 font-semibold">
3
+ <slot />
4
+ </AGHeadlessModalTitle>
5
+ </template>
6
+
7
+ <script setup lang="ts">
8
+ import AGHeadlessModalTitle from '../headless/modals/AGHeadlessModalTitle.vue';
9
+ </script>
@@ -1,5 +1,23 @@
1
+ import AGAlertModal from './AGAlertModal.vue';
2
+ import AGConfirmModal from './AGConfirmModal.vue';
3
+ import AGErrorReportModalButtons from './AGErrorReportModalButtons.vue';
4
+ import AGErrorReportModalTitle from './AGErrorReportModalTitle.vue';
5
+ import AGLoadingModal from './AGLoadingModal.vue';
1
6
  import AGModal from './AGModal.vue';
7
+ import AGModalTitle from './AGModalTitle.vue';
2
8
  import AGModalContext from './AGModalContext.vue';
3
- import { IAGModal } from './AGModal';
4
9
 
5
- export { AGModal, AGModalContext, IAGModal };
10
+ export * from './AGErrorReportModal';
11
+ export * from './AGModal';
12
+ export * from './AGModalContext';
13
+
14
+ export {
15
+ AGAlertModal,
16
+ AGConfirmModal,
17
+ AGErrorReportModalButtons,
18
+ AGErrorReportModalTitle,
19
+ AGLoadingModal,
20
+ AGModal,
21
+ AGModalTitle,
22
+ AGModalContext,
23
+ };
@@ -0,0 +1,42 @@
1
+ <template>
2
+ <AGHeadlessSnackbar class="flex flex-row items-center justify-center gap-3 p-4" :class="styleClasses">
3
+ <AGMarkdown :text="message" inline />
4
+ <AGButton
5
+ v-for="(action, i) of actions"
6
+ :key="i"
7
+ :color="color"
8
+ @click="activate(action)"
9
+ >
10
+ {{ action.text }}
11
+ </AGButton>
12
+ </AGHeadlessSnackbar>
13
+ </template>
14
+
15
+ <script setup lang="ts">
16
+ import { computed } from 'vue';
17
+
18
+ import UI from '@/ui/UI';
19
+ import { Colors } from '@/components/constants';
20
+ import { useSnackbarProps } from '@/components/headless';
21
+ import type { SnackbarAction } from '@/components/headless';
22
+
23
+ import AGButton from '../forms/AGButton.vue';
24
+ import AGHeadlessSnackbar from '../headless/snackbars/AGHeadlessSnackbar.vue';
25
+ import AGMarkdown from '../basic/AGMarkdown.vue';
26
+
27
+ const props = defineProps(useSnackbarProps());
28
+ const styleClasses = computed(() => {
29
+ switch (props.color) {
30
+ case Colors.Danger:
31
+ return 'bg-red-200 text-red-900';
32
+ default:
33
+ case Colors.Secondary:
34
+ return 'bg-gray-900 text-white';
35
+ }
36
+ });
37
+
38
+ function activate(action: SnackbarAction): void {
39
+ action.handler?.();
40
+ action.dismiss && UI.hideSnackbar(props.id);
41
+ }
42
+ </script>
@@ -0,0 +1,3 @@
1
+ import AGSnackbar from './AGSnackbar.vue';
2
+
3
+ export { AGSnackbar };
@@ -1,13 +1,28 @@
1
1
  import type { Directive } from 'vue';
2
2
 
3
- import { defineBootstrapHook } from '@/bootstrap/hooks';
3
+ import { definePlugin } from '@/plugins';
4
4
 
5
5
  import initialFocus from './initial-focus';
6
6
 
7
- const directives: Record<string, Directive> = {
7
+ const builtInDirectives: Record<string, Directive> = {
8
8
  'initial-focus': initialFocus,
9
9
  };
10
10
 
11
- export default defineBootstrapHook(async (app) => {
12
- Object.entries(directives).forEach(([name, directive]) => app.directive(name, directive));
11
+ export default definePlugin({
12
+ install(app, options) {
13
+ const directives = {
14
+ ...builtInDirectives,
15
+ ...options.directives,
16
+ };
17
+
18
+ for (const [name, directive] of Object.entries(directives)) {
19
+ app.directive(name, directive);
20
+ }
21
+ },
13
22
  });
23
+
24
+ declare module '@/bootstrap/options' {
25
+ interface AerogelOptions {
26
+ directives?: Record<string, Directive>;
27
+ }
28
+ }
@@ -0,0 +1,31 @@
1
+ import type { JSError } from '@noeldemartin/utils';
2
+
3
+ import { defineServiceState } from '@/services';
4
+
5
+ export type ErrorSource = string | Error | JSError | unknown;
6
+
7
+ export interface ErrorReport {
8
+ title: string;
9
+ description?: string;
10
+ details?: string;
11
+ error?: Error | JSError | unknown;
12
+ }
13
+
14
+ export interface ErrorReportLog {
15
+ report: ErrorReport;
16
+ seen: boolean;
17
+ date: Date;
18
+ }
19
+
20
+ export default defineServiceState({
21
+ name: 'errors',
22
+ initialState: {
23
+ logs: [] as ErrorReportLog[],
24
+ startupErrors: [] as ErrorReport[],
25
+ },
26
+ computed: {
27
+ hasErrors: ({ logs }) => logs.length > 0,
28
+ hasNewErrors: ({ logs }) => logs.some((error) => !error.seen),
29
+ hasStartupErrors: ({ startupErrors }) => startupErrors.length > 0,
30
+ },
31
+ });
@@ -0,0 +1,183 @@
1
+ import { JSError, facade, isObject, objectWithoutEmpty, toString } from '@noeldemartin/utils';
2
+
3
+ import App from '@/services/App';
4
+ import ServiceBootError from '@/errors/ServiceBootError';
5
+ import UI, { UIComponents } from '@/ui/UI';
6
+ import { translateWithDefault } from '@/lang/utils';
7
+
8
+ import Service from './Errors.state';
9
+ import { Colors } from '@/components/constants';
10
+ import type { ErrorReport, ErrorReportLog, ErrorSource } from './Errors.state';
11
+
12
+ export class ErrorsService extends Service {
13
+
14
+ public forceReporting: boolean = false;
15
+ private enabled: boolean = true;
16
+
17
+ public enable(): void {
18
+ this.enabled = true;
19
+ }
20
+
21
+ public disable(): void {
22
+ this.enabled = false;
23
+ }
24
+
25
+ public async inspect(error: ErrorSource | ErrorReport[]): Promise<void> {
26
+ const reports = Array.isArray(error) ? error : [await this.createErrorReport(error)];
27
+
28
+ if (reports.length === 0) {
29
+ UI.alert(translateWithDefault('errors.inspectEmpty', 'Nothing to inspect!'));
30
+
31
+ return;
32
+ }
33
+
34
+ UI.openModal(UI.requireComponent(UIComponents.ErrorReportModal), { reports });
35
+ }
36
+
37
+ public async report(error: ErrorSource, message?: string): Promise<void> {
38
+ if (App.development || App.testing) {
39
+ this.logError(error);
40
+ }
41
+
42
+ if (!this.enabled) {
43
+ throw error;
44
+ }
45
+
46
+ if (!App.isMounted) {
47
+ const startupError = await this.createStartupErrorReport(error);
48
+
49
+ if (startupError) {
50
+ this.setState({ startupErrors: this.startupErrors.concat(startupError) });
51
+ }
52
+
53
+ return;
54
+ }
55
+
56
+ const report = await this.createErrorReport(error);
57
+ const log: ErrorReportLog = {
58
+ report,
59
+ seen: false,
60
+ date: new Date(),
61
+ };
62
+
63
+ UI.showSnackbar(
64
+ message ??
65
+ translateWithDefault('errors.notice', 'Something went wrong, but it\'s not your fault. Try again!'),
66
+ {
67
+ color: Colors.Danger,
68
+ actions: [
69
+ {
70
+ text: translateWithDefault('errors.viewDetails', 'View details'),
71
+ dismiss: true,
72
+ handler: () =>
73
+ UI.openModal(UI.requireComponent(UIComponents.ErrorReportModal), {
74
+ reports: [report],
75
+ }),
76
+ },
77
+ ],
78
+ },
79
+ );
80
+
81
+ this.setState({ logs: [log].concat(this.logs) });
82
+ }
83
+
84
+ public see(report: ErrorReport): void {
85
+ this.setState({
86
+ logs: this.logs.map((log) => {
87
+ if (log.report !== report) {
88
+ return log;
89
+ }
90
+
91
+ return {
92
+ ...log,
93
+ seen: true,
94
+ };
95
+ }),
96
+ });
97
+ }
98
+
99
+ public seeAll(): void {
100
+ this.setState({
101
+ logs: this.logs.map((log) => ({
102
+ ...log,
103
+ seen: true,
104
+ })),
105
+ });
106
+ }
107
+
108
+ public getErrorMessage(error: ErrorSource): string {
109
+ if (typeof error === 'string') {
110
+ return error;
111
+ }
112
+
113
+ if (error instanceof Error || error instanceof JSError) {
114
+ return error.message;
115
+ }
116
+
117
+ if (isObject(error)) {
118
+ return toString(error['message'] ?? error['description'] ?? 'Unknown error object');
119
+ }
120
+
121
+ return translateWithDefault('errors.unknown', 'Unknown Error');
122
+ }
123
+
124
+ private logError(error: unknown): void {
125
+ // eslint-disable-next-line no-console
126
+ console.error(error);
127
+
128
+ if (isObject(error) && error.cause) {
129
+ this.logError(error.cause);
130
+ }
131
+ }
132
+
133
+ private async createErrorReport(error: ErrorSource): Promise<ErrorReport> {
134
+ if (typeof error === 'string') {
135
+ return { title: error };
136
+ }
137
+
138
+ if (error instanceof Error || error instanceof JSError) {
139
+ return this.createErrorReportFromError(error);
140
+ }
141
+
142
+ if (isObject(error)) {
143
+ return objectWithoutEmpty({
144
+ title: toString(
145
+ error['name'] ?? error['title'] ?? translateWithDefault('errors.unknown', 'Unknown Error'),
146
+ ),
147
+ description: toString(
148
+ error['message'] ??
149
+ error['description'] ??
150
+ translateWithDefault('errors.unknownDescription', 'Unknown error object'),
151
+ ),
152
+ error,
153
+ });
154
+ }
155
+
156
+ return {
157
+ title: translateWithDefault('errors.unknown', 'Unknown Error'),
158
+ error,
159
+ };
160
+ }
161
+
162
+ private async createStartupErrorReport(error: ErrorSource): Promise<ErrorReport | null> {
163
+ if (error instanceof ServiceBootError) {
164
+ // Ignore second-order boot errors in order to have a cleaner startup crash screen.
165
+ return error.cause instanceof ServiceBootError ? null : this.createErrorReport(error.cause);
166
+ }
167
+
168
+ return this.createErrorReport(error);
169
+ }
170
+
171
+ private createErrorReportFromError(error: Error | JSError, defaults: Partial<ErrorReport> = {}): ErrorReport {
172
+ return {
173
+ title: error.name,
174
+ description: error.message,
175
+ details: error.stack,
176
+ error,
177
+ ...defaults,
178
+ };
179
+ }
180
+
181
+ }
182
+
183
+ export default facade(new ErrorsService());
@@ -0,0 +1,59 @@
1
+ import { tap } from '@noeldemartin/utils';
2
+
3
+ import { bootServices } from '@/services';
4
+ import { definePlugin } from '@/plugins';
5
+
6
+ import Errors from './Errors';
7
+ import { ErrorReport, ErrorReportLog, ErrorSource } from './Errors.state';
8
+
9
+ export { Errors, ErrorSource, ErrorReport, ErrorReportLog };
10
+
11
+ const services = { $errors: Errors };
12
+ const frameworkHandler: ErrorHandler = (error) => {
13
+ if (!Errors.instance) {
14
+ // eslint-disable-next-line no-console
15
+ console.warn('Errors service hasn\'t been initialized properly!');
16
+
17
+ // eslint-disable-next-line no-console
18
+ console.error(error);
19
+
20
+ return true;
21
+ }
22
+
23
+ Errors.report(error);
24
+
25
+ return true;
26
+ };
27
+
28
+ function setUpErrorHandler(baseHandler: ErrorHandler = () => false): ErrorHandler {
29
+ return tap(
30
+ (error) => baseHandler(error) || frameworkHandler(error),
31
+ (errorHandler) => {
32
+ globalThis.onerror = (message, _, __, ___, error) => errorHandler(error ?? message);
33
+ globalThis.onunhandledrejection = (event) => errorHandler(event.reason);
34
+ },
35
+ );
36
+ }
37
+
38
+ export type ErrorHandler = (error: ErrorSource) => boolean;
39
+ export type ErrorsServices = typeof services;
40
+
41
+ export default definePlugin({
42
+ async install(app, options) {
43
+ const errorHandler = setUpErrorHandler(options.handleError);
44
+
45
+ app.config.errorHandler = errorHandler;
46
+
47
+ await bootServices(app, services);
48
+ },
49
+ });
50
+
51
+ declare module '@/bootstrap/options' {
52
+ interface AerogelOptions {
53
+ handleError?(error: ErrorSource): boolean;
54
+ }
55
+ }
56
+
57
+ declare module '@/services' {
58
+ export interface Services extends ErrorsServices {}
59
+ }