mirror of
				https://github.com/TriliumNext/Notes.git
				synced 2025-10-31 04:51:31 +08:00 
			
		
		
		
	error handling in custom request handler
This commit is contained in:
		
							parent
							
								
									d3ca6b5ae6
								
							
						
					
					
						commit
						bad7b84993
					
				| @ -102,6 +102,6 @@ body { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| ::-webkit-scrollbar-thumb { | ::-webkit-scrollbar-thumb { | ||||||
|     border-radius: 5px; |     border-radius: 3px; | ||||||
|     -webkit-box-shadow: inset 0 0 3px var(--main-border-color); |     border: 1px solid var(--main-border-color); | ||||||
| } | } | ||||||
| @ -9,7 +9,7 @@ | |||||||
| 
 | 
 | ||||||
|     --main-background-color: white; |     --main-background-color: white; | ||||||
|     --main-text-color: black; |     --main-text-color: black; | ||||||
|     --main-border-color: #ddd; |     --main-border-color: #ccc; | ||||||
|     --accented-background-color: #eee; |     --accented-background-color: #eee; | ||||||
|     --more-accented-background-color: #ccc; |     --more-accented-background-color: #ccc; | ||||||
|     --header-background-color: #f8f8f8; |     --header-background-color: #f8f8f8; | ||||||
| @ -345,6 +345,10 @@ div.ui-tooltip { | |||||||
|     padding: 5px 5px 5px 15px; |     padding: 5px 5px 5px 15px; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | #search-text { | ||||||
|  |     border: 1px solid var(--main-border-color); | ||||||
|  | } | ||||||
|  | 
 | ||||||
| /* | /* | ||||||
| * .electron-in-page-search-window is a class specified to default | * .electron-in-page-search-window is a class specified to default | ||||||
| * <webview> element for search window. | * <webview> element for search window. | ||||||
|  | |||||||
| @ -31,11 +31,18 @@ function register(router) { | |||||||
| 
 | 
 | ||||||
|                 log.info(`Handling custom request "${path}" with note ${note.noteId}`); |                 log.info(`Handling custom request "${path}" with note ${note.noteId}`); | ||||||
| 
 | 
 | ||||||
|                 await scriptService.executeNote(note, { |                 try { | ||||||
|                     pathParams: match.slice(1), |                     await scriptService.executeNote(note, { | ||||||
|                     req, |                         pathParams: match.slice(1), | ||||||
|                     res |                         req, | ||||||
|                 }); |                         res | ||||||
|  |                     }); | ||||||
|  |                 } | ||||||
|  |                 catch (e) { | ||||||
|  |                     log.error(`Custom handler ${note.noteId} failed with ${e.message}`); | ||||||
|  | 
 | ||||||
|  |                     res.status(500).send(e.message); | ||||||
|  |                 } | ||||||
|             } |             } | ||||||
|             else if (attr.name === 'customResourceProvider') { |             else if (attr.name === 'customResourceProvider') { | ||||||
|                 await fileUploadService.downloadNoteFile(attr.noteId, res); |                 await fileUploadService.downloadNoteFile(attr.noteId, res); | ||||||
|  | |||||||
| @ -173,6 +173,23 @@ function BackendScriptApi(currentNote, apiParams) { | |||||||
|      */ |      */ | ||||||
|     this.createNote = noteService.createNote; |     this.createNote = noteService.createNote; | ||||||
| 
 | 
 | ||||||
|  |     /** | ||||||
|  |      * Creates new note according to given params and force all connected clients to refresh their tree. | ||||||
|  |      * | ||||||
|  |      * @method | ||||||
|  |      * | ||||||
|  |      * @param {string} parentNoteId - create new note under this parent | ||||||
|  |      * @param {string} title | ||||||
|  |      * @param {string} [content=""] | ||||||
|  |      * @param {CreateNoteExtraOptions} [extraOptions={}] | ||||||
|  |      * @returns {Promise<{note: Note, branch: Branch}>} object contains newly created entities note and branch | ||||||
|  |      */ | ||||||
|  |     this.createNoteAndRefresh = async function(parentNoteId, title, content, extraOptions) { | ||||||
|  |         await noteService.createNote(parentNoteId, title, content, extraOptions); | ||||||
|  | 
 | ||||||
|  |         messagingService.refreshTree(); | ||||||
|  |     }; | ||||||
|  | 
 | ||||||
|     /** |     /** | ||||||
|      * Log given message to trilium logs. |      * Log given message to trilium logs. | ||||||
|      * |      * | ||||||
| @ -238,7 +255,7 @@ function BackendScriptApi(currentNote, apiParams) { | |||||||
|      * |      * | ||||||
|      * @returns {Promise<void>} |      * @returns {Promise<void>} | ||||||
|      */ |      */ | ||||||
|     this.refreshTree = () => messagingService.sendMessageToAllClients({ type: 'refresh-tree' }); |     this.refreshTree = messagingService.refreshTree; | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
|      * @return {{syncVersion, appVersion, buildRevision, dbVersion, dataDirectory, buildDate}|*} - object representing basic info about running Trilium version |      * @return {{syncVersion, appVersion, buildRevision, dbVersion, dataDirectory, buildDate}|*} - object representing basic info about running Trilium version | ||||||
|  | |||||||
| @ -432,7 +432,7 @@ async function runChecks() { | |||||||
|     }); |     }); | ||||||
| 
 | 
 | ||||||
|     if (fixedIssues) { |     if (fixedIssues) { | ||||||
|         messagingService.sendMessageToAllClients({ type: 'refresh-tree' }); |         messagingService.refreshTree(); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     if (unrecoverableConsistencyErrors) { |     if (unrecoverableConsistencyErrors) { | ||||||
|  | |||||||
| @ -12,7 +12,7 @@ async function runAttachedRelations(note, relationName, originEntity) { | |||||||
|         const scriptNote = await relation.getTargetNote(); |         const scriptNote = await relation.getTargetNote(); | ||||||
| 
 | 
 | ||||||
|         if (scriptNote) { |         if (scriptNote) { | ||||||
|             await scriptService.executeNote(scriptNote, { originEntity }); |             await scriptService.executeNoteNoException(scriptNote, { originEntity }); | ||||||
|         } |         } | ||||||
|         else { |         else { | ||||||
|             log.error(`Target note ${relation.value} of atttribute ${relation.attributeId} has not been found.`); |             log.error(`Target note ${relation.value} of atttribute ${relation.attributeId} has not been found.`); | ||||||
| @ -30,7 +30,7 @@ eventService.subscribe(eventService.NOTE_TITLE_CHANGED, async note => { | |||||||
|             if (await parent.hasLabel("sorted")) { |             if (await parent.hasLabel("sorted")) { | ||||||
|                 await treeService.sortNotesAlphabetically(parent.noteId); |                 await treeService.sortNotesAlphabetically(parent.noteId); | ||||||
| 
 | 
 | ||||||
|                 messagingService.sendMessageToAllClients({ type: 'refresh-tree' }); |                 messagingService.refreshTree(); | ||||||
|                 break; // sending the message once is enough
 |                 break; // sending the message once is enough
 | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|  | |||||||
| @ -49,6 +49,10 @@ async function sendMessage(client, message) { | |||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | async function refreshTree() { | ||||||
|  |     await sendMessageToAllClients({ type: 'refresh-tree' }); | ||||||
|  | } | ||||||
|  | 
 | ||||||
| async function sendMessageToAllClients(message) { | async function sendMessageToAllClients(message) { | ||||||
|     const jsonStr = JSON.stringify(message); |     const jsonStr = JSON.stringify(message); | ||||||
| 
 | 
 | ||||||
| @ -76,5 +80,6 @@ async function sendPing(client, lastSentSyncId) { | |||||||
| 
 | 
 | ||||||
| module.exports = { | module.exports = { | ||||||
|     init, |     init, | ||||||
|  |     refreshTree, | ||||||
|     sendMessageToAllClients |     sendMessageToAllClients | ||||||
| }; | }; | ||||||
| @ -17,7 +17,7 @@ async function runNotesWithLabel(runAttrValue) { | |||||||
|           AND notes.isDeleted = 0`, [runAttrValue]);
 |           AND notes.isDeleted = 0`, [runAttrValue]);
 | ||||||
| 
 | 
 | ||||||
|     for (const note of notes) { |     for (const note of notes) { | ||||||
|         scriptService.executeNote(note, { originEntity: note }); |         scriptService.executeNoteNoException(note, { originEntity: note }); | ||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -15,6 +15,15 @@ async function executeNote(note, apiParams) { | |||||||
|     await executeBundle(bundle, apiParams); |     await executeBundle(bundle, apiParams); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | async function executeNoteNoException(note, apiParams) { | ||||||
|  |     try { | ||||||
|  |         await executeNote(note, apiParams); | ||||||
|  |     } | ||||||
|  |     catch (e) { | ||||||
|  |         // just swallow, exception is logged already in executeNote
 | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
| async function executeBundle(bundle, apiParams = {}) { | async function executeBundle(bundle, apiParams = {}) { | ||||||
|     if (!apiParams.startNote) { |     if (!apiParams.startNote) { | ||||||
|         // this is the default case, the only exception is when we want to preserve frontend startNote
 |         // this is the default case, the only exception is when we want to preserve frontend startNote
 | ||||||
| @ -36,6 +45,8 @@ async function executeBundle(bundle, apiParams = {}) { | |||||||
|     } |     } | ||||||
|     catch (e) { |     catch (e) { | ||||||
|         log.error(`Execution of script "${bundle.note.title}" (${bundle.note.noteId}) failed with error: ${e.message}`); |         log.error(`Execution of script "${bundle.note.title}" (${bundle.note.noteId}) failed with error: ${e.message}`); | ||||||
|  | 
 | ||||||
|  |         throw e; | ||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| @ -168,6 +179,7 @@ function sanitizeVariableName(str) { | |||||||
| 
 | 
 | ||||||
| module.exports = { | module.exports = { | ||||||
|     executeNote, |     executeNote, | ||||||
|  |     executeNoteNoException, | ||||||
|     executeScript, |     executeScript, | ||||||
|     getScriptBundleForFrontend |     getScriptBundleForFrontend | ||||||
| }; | }; | ||||||
| @ -76,7 +76,7 @@ | |||||||
| 
 | 
 | ||||||
|         <div id="search-box"> |         <div id="search-box"> | ||||||
|             <div style="display: flex; align-items: center; flex-wrap: wrap;"> |             <div style="display: flex; align-items: center; flex-wrap: wrap;"> | ||||||
|                 <input name="search-text" placeholder="Search text, labels" style="flex-grow: 100; margin-left: 5px; margin-right: 5px; flex-basis: 5em; min-width: 0;" autocomplete="off"> |                 <input name="search-text" id="search-text" placeholder="Search text, labels" style="flex-grow: 100; margin-left: 5px; margin-right: 5px; flex-basis: 5em; min-width: 0;" autocomplete="off"> | ||||||
|                 <button id="do-search-button" class="btn btn-sm icon-button jam jam-search" title="Search (enter)"></button> |                 <button id="do-search-button" class="btn btn-sm icon-button jam jam-search" title="Search (enter)"></button> | ||||||
| 
 | 
 | ||||||
|                   |                   | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 zadam
						zadam