2020-06-18 07:31:30 -05:00
|
|
|
import React from 'react';
|
2022-04-22 08:33:13 -05:00
|
|
|
|
2020-06-18 07:31:30 -05:00
|
|
|
import { DataQueryError } from '@grafana/data';
|
2023-04-05 02:13:24 -05:00
|
|
|
import { config } from '@grafana/runtime';
|
2023-03-08 09:11:38 -06:00
|
|
|
import { Alert, JSONFormatter } from '@grafana/ui';
|
2020-06-18 07:31:30 -05:00
|
|
|
|
|
|
|
interface InspectErrorTabProps {
|
2023-03-08 09:11:38 -06:00
|
|
|
errors?: DataQueryError[];
|
2020-06-18 07:31:30 -05:00
|
|
|
}
|
|
|
|
|
2022-02-18 05:09:24 -06:00
|
|
|
const parseErrorMessage = (message: string): { msg: string; json?: any } => {
|
|
|
|
try {
|
|
|
|
const [msg, json] = message.split(/(\{.+)/);
|
|
|
|
const jsonError = JSON.parse(json);
|
|
|
|
return {
|
|
|
|
msg,
|
|
|
|
json: jsonError,
|
|
|
|
};
|
|
|
|
} catch {
|
|
|
|
return { msg: message };
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-03-08 09:11:38 -06:00
|
|
|
function renderError(error: DataQueryError) {
|
2020-06-18 07:31:30 -05:00
|
|
|
if (error.data) {
|
|
|
|
return (
|
|
|
|
<>
|
2022-03-16 11:28:09 -05:00
|
|
|
<h4>{error.data.message}</h4>
|
2020-06-18 07:31:30 -05:00
|
|
|
<JSONFormatter json={error} open={2} />
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
2022-02-18 05:09:24 -06:00
|
|
|
if (error.message) {
|
|
|
|
const { msg, json } = parseErrorMessage(error.message);
|
|
|
|
if (!json) {
|
2023-03-08 09:11:38 -06:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{error.status && <>Status: {error.status}. Message: </>}
|
|
|
|
{msg}
|
2023-04-05 02:13:24 -05:00
|
|
|
{config.featureToggles.showTraceId && error.traceId != null && (
|
|
|
|
<>
|
|
|
|
<br />
|
|
|
|
(Trace ID: {error.traceId})
|
|
|
|
</>
|
|
|
|
)}
|
2023-03-08 09:11:38 -06:00
|
|
|
</>
|
|
|
|
);
|
2022-02-18 05:09:24 -06:00
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{msg !== '' && <h3>{msg}</h3>}
|
2023-03-08 09:11:38 -06:00
|
|
|
{error.status && <>Status: {error.status}</>}
|
2022-02-18 05:09:24 -06:00
|
|
|
<JSONFormatter json={json} open={5} />
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return <JSONFormatter json={error} open={2} />;
|
2023-03-08 09:11:38 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
export const InspectErrorTab = ({ errors }: InspectErrorTabProps) => {
|
|
|
|
if (!errors?.length) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (errors.length === 1) {
|
|
|
|
return renderError(errors[0]);
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{errors.map((error, index) => (
|
|
|
|
<Alert title={error.refId || `Query ${index + 1}`} severity="error" key={index}>
|
|
|
|
{renderError(error)}
|
|
|
|
</Alert>
|
|
|
|
))}
|
|
|
|
</>
|
|
|
|
);
|
2020-06-18 07:31:30 -05:00
|
|
|
};
|