forked from dbeaver/cloudbeaver
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathuseErrorDetails.ts
More file actions
85 lines (76 loc) · 2.33 KB
/
useErrorDetails.ts
File metadata and controls
85 lines (76 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
* CloudBeaver - Cloud Database Manager
* Copyright (C) 2020-2024 DBeaver Corp and others
*
* Licensed under the Apache License, Version 2.0.
* you may not use this file except in compliance with the License.
*/
import { useState } from 'react';
import { useService } from '@cloudbeaver/core-di';
import { CommonDialogService } from '@cloudbeaver/core-dialogs';
import { DetailsError } from '@cloudbeaver/core-sdk';
import { errorOf, LoadingError } from '@cloudbeaver/core-utils';
import { ErrorDetailsDialog } from './ErrorDetailsDialog/ErrorDetailsDialog.js';
import { useTranslate } from './localization/useTranslate.js';
interface IErrorDetailsHook {
name?: string;
message?: string;
error: Error | string | null;
executionFailedMessage?: string;
details?: DetailsError;
hasDetails: boolean;
isOpen: boolean;
open: () => void;
refresh?: () => void;
errorCode?: string;
workflowId?: string;
}
type HookType =
| IErrorDetailsHook
| ({
name: string;
error: Error;
} & IErrorDetailsHook);
export function useErrorDetails(error: IErrorDetailsHook['error']): HookType {
const translate = useTranslate();
const service = useService(CommonDialogService);
const [isOpen, setIsOpen] = useState(false);
const details = errorOf(error, DetailsError);
const loadingError = errorOf(error, LoadingError);
const open = async () => {
if (!error) {
return;
}
try {
setIsOpen(true);
await service.open(ErrorDetailsDialog, error);
} finally {
setIsOpen(false);
}
};
const name = typeof error === 'string' ? translate('core_blocks_exception_message_error_message') : error?.name;
let message = (typeof error === 'string' ? error : error?.message) ?? undefined;
const executionFailedMessage = (error as any)?.executionFailedMessage as string;
const errorCode = (error as any)?.errorCode as string;
let workflowId: string | undefined;
if (typeof message === 'string') {
const match = message.match(/workflow_id:(\d+)/);
if (match) {
workflowId = match[1];
message = message.replace(/workflow_id:\d+,?\s*/g, '').trim();
}
}
return {
name,
message,
error,
details,
hasDetails: details?.hasDetails() ?? false,
isOpen,
open,
refresh: loadingError?.refresh,
executionFailedMessage,
errorCode,
workflowId,
};
}