Improves cleanup command error handling and formatting
Refactors error handling in cleanup command execution - Moves loading notice hide to finally block for consistent dismissal - Enhances error message formatting with locale-specific number formatting - Imports character and token limit constants for more dynamic error messages - Renames unused view parameter to indicate intentional non-use Improves user experience by providing more readable error notifications
This commit is contained in:
1 file changed
+8
-14
@@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
import { Editor, MarkdownView, Notice } from 'obsidian';
|
import { Editor, MarkdownView, Notice } from 'obsidian';
|
||||||
import { CleanupService } from '../services/cleanupService';
|
import { CleanupService } from '../services/cleanupService';
|
||||||
import { FreewritingCleanupSettings } from '../types';
|
import { FreewritingCleanupSettings, ANTHROPIC_LIMITS } from '../types';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Command handler for freewriting cleanup operations
|
* Command handler for freewriting cleanup operations
|
||||||
@@ -34,10 +34,10 @@ export class CleanupCommand {
|
|||||||
* notice during processing and error notices if operation fails.
|
* notice during processing and error notices if operation fails.
|
||||||
*
|
*
|
||||||
* @param editor - Obsidian editor instance
|
* @param editor - Obsidian editor instance
|
||||||
* @param view - Current markdown view
|
* @param _view - Current markdown view (unused but required by editorCallback signature)
|
||||||
* @param settings - Plugin settings for cleanup configuration
|
* @param settings - Plugin settings for cleanup configuration
|
||||||
*/
|
*/
|
||||||
async execute(editor: Editor, view: MarkdownView, settings: FreewritingCleanupSettings): Promise<void> {
|
async execute(editor: Editor, _view: MarkdownView, settings: FreewritingCleanupSettings): Promise<void> {
|
||||||
try {
|
try {
|
||||||
// Get selected text
|
// Get selected text
|
||||||
const selectedText = editor.getSelection();
|
const selectedText = editor.getSelection();
|
||||||
@@ -55,7 +55,7 @@ export class CleanupCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Show loading notice
|
// Show loading notice
|
||||||
const loadingNotice = new Notice('Cleaning up text...', 0); // 0 = persistent
|
const loadingNotice = new Notice('Cleaning up text...', 0); // persistent; hide in finally
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Process the text
|
// Process the text
|
||||||
@@ -76,14 +76,7 @@ export class CleanupCommand {
|
|||||||
// Move to end of current line and insert new content
|
// Move to end of current line and insert new content
|
||||||
editor.setCursor(insertPosition);
|
editor.setCursor(insertPosition);
|
||||||
editor.replaceRange(formattedResult, insertPosition);
|
editor.replaceRange(formattedResult, insertPosition);
|
||||||
|
|
||||||
// Clear the loading notice
|
|
||||||
loadingNotice.hide();
|
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// Clear loading notice
|
|
||||||
loadingNotice.hide();
|
|
||||||
|
|
||||||
// Handle specific error types
|
// Handle specific error types
|
||||||
if (error instanceof Error) {
|
if (error instanceof Error) {
|
||||||
if (error.message.includes('API key')) {
|
if (error.message.includes('API key')) {
|
||||||
@@ -100,7 +93,8 @@ export class CleanupCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
console.error('Cleanup command error:', error);
|
console.error('Cleanup command error:', error);
|
||||||
throw error;
|
} finally {
|
||||||
|
loadingNotice.hide();
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -155,14 +149,14 @@ export class CleanupCommand {
|
|||||||
if (!limits.withinCharacterLimit) {
|
if (!limits.withinCharacterLimit) {
|
||||||
return {
|
return {
|
||||||
isValid: false,
|
isValid: false,
|
||||||
error: `Text too long: ${limits.characterCount} characters (max: 680,000)`
|
error: `Text too long: ${limits.characterCount.toLocaleString()} characters (max: ${ANTHROPIC_LIMITS.MAX_CHARACTERS.toLocaleString()})`
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!limits.withinTokenLimit) {
|
if (!limits.withinTokenLimit) {
|
||||||
return {
|
return {
|
||||||
isValid: false,
|
isValid: false,
|
||||||
error: `Text too long: ~${limits.estimatedTokenCount} tokens (max: 200,000)`
|
error: `Text too long: ~${limits.estimatedTokenCount.toLocaleString()} tokens (max: ${ANTHROPIC_LIMITS.MAX_TOKENS.toLocaleString()})`
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user