forked from SolidOS/solid-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.ts
More file actions
38 lines (33 loc) · 1.54 KB
/
error.ts
File metadata and controls
38 lines (33 loc) · 1.54 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
/**
* Create an error message block
* @param dom The DOM on which dom.createElement will be called
* @param err The error message string to display (or an error object)
* @param backgroundColor Background color. Default: '#fee'
* @param err2 Is the second param is a string, you can put the original Error in here
* @returns A div element with the err string
*
* This will return a DOM element you can put in the UI as a notice for the user
* Meanwhile the stack is dumped to the console for the developer, so you actually know
* where it happened!
*/
/* eslint-disable no-console */
import { cancelButton } from '../widgets'
import * as style from '../style'
import styleConstants from '../styleConstants'
export function errorMessageBlock (dom: HTMLDocument, err: string | Error, backgroundColor?: string, err2?: Error): HTMLDivElement {
const div = dom.createElement('div')
/* tslint:disable-next-line */ // Too complex for TS?
// @ts-ignore
const errorObject:Error = err2 || err instanceof Error ? err : null
if (errorObject) {
console.error(`errorMessageBlock: ${errorObject} at: ${errorObject.stack || '??'}`, errorObject) // @@ pick one
div.textContent = errorObject.message
} else {
div.textContent = err as string
}
div.appendChild(cancelButton(dom, () => { if (div.parentNode) div.parentNode.removeChild(div) }))
.style = style.errorCancelButton
div.setAttribute('style', style.errorMessageBlockStyle)
div.style.backgroundColor = (backgroundColor || styleConstants.defaultErrorBackgroundColor)
return div
}