improve error handling
Make sure all errors are able to return their traces to be displayed, and that only the code appears in the pop-up, not a mix with the actual content.
This commit is contained in:
		
							parent
							
								
									76bf6cefaa
								
							
						
					
					
						commit
						e152034eeb
					
				
					 3 changed files with 81 additions and 79 deletions
				
			
		|  | @ -52,15 +52,15 @@ export default class alerts { | ||||||
| 	@param {number} ERROR_MESSAGE the custom error message | 	@param {number} ERROR_MESSAGE the custom error message | ||||||
| 	@param {boolean} critical the critical nature | 	@param {boolean} critical the critical nature | ||||||
| 	*/ | 	*/ | ||||||
| 	static error(ERROR_CODE, ERROR_MESSAGE, critical = true) { | 	static error(ERROR_CODE, ERROR_MESSAGE, ERROR_STACK, critical = true) { | ||||||
| 		(async () => { | 		(async () => { | ||||||
| 			// Import the templating.
 | 			// Import the templating.
 | ||||||
| 			const texts = (await import(chrome.runtime.getURL("gui/scripts/read.js"))).default; | 			const texts = (await import(chrome.runtime.getURL("gui/scripts/read.js"))).default; | ||||||
| 
 | 
 | ||||||
| 			// Display the error message.
 | 			// Display the error message.
 | ||||||
| 			console.error(texts.localized(`error_msg`, null, [ERROR_CODE, ERROR_MESSAGE])); | 			console.error(texts.localized(`error_msg`, false, [ERROR_CODE, ERROR_MESSAGE, ERROR_STACK])); | ||||||
| 			if (critical) { | 			if (critical) { | ||||||
| 				alert(texts.localized(`error_msg_GUI`, null, [String(ERROR_CODE)])); | 				alert(texts.localized(`error_msg_GUI`, false, [String(ERROR_CODE)])); | ||||||
| 			} else { | 			} else { | ||||||
| 				try { | 				try { | ||||||
| 					M.toast({ text: message }); | 					M.toast({ text: message }); | ||||||
|  |  | ||||||
|  | @ -18,57 +18,60 @@ export default class windowman { | ||||||
| 
 | 
 | ||||||
|   // Prepare the window with its metadata.
 |   // Prepare the window with its metadata.
 | ||||||
|   constructor() { |   constructor() { | ||||||
|     function headers() { | 	function headers() { | ||||||
|       let LOAD_STATE = true; | 		let LOAD_STATE = true; | ||||||
|       let UI = { | 		let UI = { | ||||||
|         CSS: [ | 			CSS: [ | ||||||
|           chrome.runtime.getURL( | 				chrome.runtime.getURL("gui/styles/external/fonts/materialdesignicons.min.css"), | ||||||
|             "gui/styles/external/fonts/materialdesignicons.min.css", | 				chrome.runtime.getURL("gui/styles/external/materialize/css/materialize.css"), | ||||||
|           ), | 				chrome.runtime.getURL("gui/styles/ui.css"), | ||||||
|           chrome.runtime.getURL( | 			] | ||||||
|             "gui/styles/external/materialize/css/materialize.css", | 		}; | ||||||
|           ), |  | ||||||
|           chrome.runtime.getURL("gui/styles/ui.css"), |  | ||||||
|         ], |  | ||||||
|       }; |  | ||||||
| 
 | 
 | ||||||
|       for (let index = 0; index < UI[`CSS`].length; index++) { | 		for (let index = 0; index < UI[`CSS`].length; index++) { | ||||||
|         const source = UI.CSS[index]; | 			const source = UI.CSS[index]; | ||||||
| 
 | 
 | ||||||
|         (async () => { | 			try { | ||||||
|           const net = await import(chrome.runtime.getURL(`/scripts/net.js`)); | 				(async () => { | ||||||
|  | 					// Import source reading for later. 
 | ||||||
|  | 					const reader = (await import(chrome.runtime.getURL(`/gui/scripts/read.js`))).default; | ||||||
|  | 					 | ||||||
|  | 					const net = await import(chrome.runtime.getURL(`/scripts/net.js`)); | ||||||
| 
 | 
 | ||||||
|           let resource = false; | 					let resource = false; | ||||||
|           try { | 					try { | ||||||
|             resource = await net.download(source, `text`, true); | 					resource = await net.download(source, `text`, true); | ||||||
|           } catch (err) {} | 					} catch (err) {} | ||||||
| 
 | 
 | ||||||
|           if (resource) { | 					if (resource) { | ||||||
|             let metadata_element = document.createElement(`link`); | 						let metadata_element = document.createElement(`link`); | ||||||
|             metadata_element.setAttribute(`rel`, `stylesheet`); | 						metadata_element.setAttribute(`rel`, `stylesheet`); | ||||||
|             metadata_element.setAttribute(`type`, `text/css`); | 						metadata_element.setAttribute(`type`, `text/css`); | ||||||
|             metadata_element.setAttribute(`href`, source); | 						metadata_element.setAttribute(`href`, source); | ||||||
|             document.querySelector(`head`).appendChild(metadata_element); | 						document.querySelector(`head`).appendChild(metadata_element); | ||||||
|           } else { | 					} else { | ||||||
|             const alerts = ( | 						throw new ReferenceError(reader.localized(`error_msg_fileNotFound`)); | ||||||
|               await import(chrome.runtime.getURL(`/gui/scripts/alerts.js`)) | 					} | ||||||
|             ).default; | 					 | ||||||
|             const reader = ( | 				})(); | ||||||
|               await import(chrome.runtime.getURL(`/gui/scripts/read.js`)) | 			} catch(err) { | ||||||
|             ).default; | 				(async() => { | ||||||
|  | 					const alerts = (await import(chrome.runtime.getURL(`/gui/scripts/alerts.js`))).default; | ||||||
|  | 					 | ||||||
|  | 					// Raise an alert. 
 | ||||||
|  | 					alerts.error(err.name, err.message, err.stack, true, [source]); | ||||||
| 
 | 
 | ||||||
|             alerts.error(-1, reader.localized(`error_msg_fileNotFound`), true, [ | 					// Stop loading the page when an error has occured; it's not going to work!
 | ||||||
|               source, | 					if (!DEBUG) { | ||||||
|             ]); | 						window.close(); | ||||||
| 
 | 					} | ||||||
|             // Stop loading the page when an error has occured; it's not going to work!
 | 				})(); | ||||||
|             if (!DEBUG) { | 				 | ||||||
|               window.close(); | 				// Stop loading immediately during the error. 
 | ||||||
|             } | 				break; | ||||||
|           } | 			}; | ||||||
|         })(); | 		} | ||||||
|       } | 	} | ||||||
|     } |  | ||||||
| 
 | 
 | ||||||
|     // Get the window.
 |     // Get the window.
 | ||||||
|     this[`metadata`] = chrome.windows.getCurrent(); |     this[`metadata`] = chrome.windows.getCurrent(); | ||||||
|  | @ -453,11 +456,6 @@ export default class windowman { | ||||||
|             input_elements.forEach((input_element) => { |             input_elements.forEach((input_element) => { | ||||||
|               if (input_element.getAttribute("data-enable")) { |               if (input_element.getAttribute("data-enable")) { | ||||||
|                 (async () => { |                 (async () => { | ||||||
|                   console.log( |  | ||||||
|                     await secretariat.read( |  | ||||||
|                       input_element.getAttribute("data-enable"), |  | ||||||
|                     ), |  | ||||||
|                   ); |  | ||||||
|                   input_element.disabled = !((await secretariat.read( |                   input_element.disabled = !((await secretariat.read( | ||||||
|                     input_element.getAttribute("data-enable"), |                     input_element.getAttribute("data-enable"), | ||||||
|                   )) != null |                   )) != null | ||||||
|  |  | ||||||
|  | @ -1,5 +1,5 @@ | ||||||
| /* net.js | /* net.js | ||||||
|   This script provides network utilities. | 	This script provides network utilities. | ||||||
| */ | */ | ||||||
| 
 | 
 | ||||||
| /* | /* | ||||||
|  | @ -11,31 +11,35 @@ Download a file from the network or locally. | ||||||
| @returns {Promise} the downloaded file | @returns {Promise} the downloaded file | ||||||
| */ | */ | ||||||
| export async function download(URL, type, verify_only = false) { | export async function download(URL, type, verify_only = false) { | ||||||
|   const alert = await import(chrome.runtime.getURL(`gui/scripts/alerts.js`)) | 	const alert = await import(chrome.runtime.getURL(`gui/scripts/alerts.js`)) | ||||||
|     .default; | 		.default; | ||||||
|   const texts = (await import(chrome.runtime.getURL(`gui/scripts/read.js`))) | 	const texts = (await import(chrome.runtime.getURL(`gui/scripts/read.js`))) | ||||||
|     .default; | 		.default; | ||||||
| 
 | 
 | ||||||
|   let connect = await fetch(URL), | 	let connect = await fetch(URL), | ||||||
|     data; | 		data; | ||||||
| 
 | 
 | ||||||
|   if (connect.ok && !verify_only) { | 	if (connect.ok && !verify_only) { | ||||||
|     data = await connect.text(); | 		try { | ||||||
|  | 			data = await connect.text(); | ||||||
|  | 		 | ||||||
|  | 			if ( | ||||||
|  | 				type | ||||||
|  | 					? type.toLowerCase().includes(`json`) || type.toLowerCase().includes(`dictionary`) | ||||||
|  | 					: false | ||||||
|  | 			) { | ||||||
|  | 				try { | ||||||
|  | 				data = JSON.parse(data); | ||||||
|  | 				// When not in JSON, run this.
 | ||||||
|  | 				} catch(err) { | ||||||
|  | 					throw new TypeError(texts.localized(`error_msg_notJSON`, false)); | ||||||
|  | 				}; | ||||||
|  | 			}; | ||||||
|  | 		} catch(err) { | ||||||
|  | 			alert.error(err.name, err.message, err.stack); | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
| 
 | 
 | ||||||
|     try { | 	// Return the filter.
 | ||||||
|     } catch (err) { | 	return verify_only ? connect.ok : data; | ||||||
|       if ( |  | ||||||
|         type |  | ||||||
|           ? type.toLowerCase().includes(`json`) || |  | ||||||
|             type.toLowerCase().includes(`dictionary`) |  | ||||||
|           : false |  | ||||||
|       ) { |  | ||||||
|         data = JSON.parse(data); |  | ||||||
|       } |  | ||||||
|       alert.error(texts.localized(`error_msg_notJSON`, false)); |  | ||||||
|     } |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   // Return the filter.
 |  | ||||||
|   return verify_only ? connect.ok : data; |  | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue