Path: blob/master/src/packages/frontend/components/error-display.tsx
1503 views
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { Alert } from "antd";67// use "style" to customize8const ELEMENT_STYLE: React.CSSProperties = {9overflowY: "auto",10} as const;1112// use "body_style" prop to customize13const BODY_STYLE: React.CSSProperties = {14marginRight: "10px",15whiteSpace: "pre-wrap",16} as const;1718interface Props {19error?: string | object;20error_component?: React.JSX.Element | React.JSX.Element[];21title?: string;22style?: React.CSSProperties;23body_style?: React.CSSProperties;24componentStyle?: React.CSSProperties;25bsStyle?: string;26onClose?: () => void;27banner?: boolean;28}2930export function ErrorDisplay({31error,32error_component,33title,34body_style,35componentStyle,36style,37bsStyle,38onClose,39banner = false,40}: Props) {41function render_title() {42return <h4>{title}</h4>;43}4445function render_error() {46if (error) {47let e = typeof error == "string" ? error : `${error}`;48// common prefix with errors due to how they get constructed49while (e.startsWith("Error: Error")) {50e = e.slice("Error: ".length);51}52return e;53} else {54return error_component;55}56}5758function type(): string {59if (60// only types that antd has...61bsStyle != null &&62["success", "info", "warning", "error"].includes(bsStyle)63) {64return bsStyle;65} else {66return "error";67}68}6970function msgdesc() {71const body = (72<div style={{ ...BODY_STYLE, ...body_style }}>{render_error()}</div>73);74if (title) {75return [render_title(), body];76} else {77return [body, undefined];78}79}8081function render_alert() {82const [message, description] = msgdesc();83// tweak the case where it's not a banner84const extra = banner ? undefined : { closable: true, onClose };85return (86<Alert87banner={banner}88showIcon89style={{ ...ELEMENT_STYLE, ...style }}90type={type() as any}91message={message}92description={description}93onClose={onClose}94closable={onClose != null || banner}95{...extra}96/>97);98}99100return <div style={componentStyle}>{render_alert()}</div>;101}102103104