Skip to content

Commit

Permalink
chore: log a warning instead of an error when encountering issues fet…
Browse files Browse the repository at this point in the history
…ching or processing completions

This change ensures the editor remains responsive, even if monacopilot can't provide completions.
  • Loading branch information
arshad-yaseen committed Dec 23, 2024
1 parent 3573876 commit 17fefee
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 19 deletions.
6 changes: 2 additions & 4 deletions src/core/completion/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {CompletionFormatter} from '../../classes';
import {CompletionCache} from '../../classes/completion-cache';
import {CompletionRange} from '../../classes/completion-range';
import {constructCompletionMetadata, fetchCompletionItem} from '../../helpers';
import {report} from '../../logger';
import {warn} from '../../logger';
import {
CompletionMetadata,
EditorInlineCompletionsResult,
Expand Down Expand Up @@ -113,8 +113,6 @@ const handleInlineCompletions = async ({
},
});

console.log('completion', completion);

if (completion) {
const formattedCompletion = CompletionFormatter.create(completion)
.removeMarkdownCodeSyntax()
Expand Down Expand Up @@ -153,7 +151,7 @@ const handleInlineCompletions = async ({
} else if (onError) {
onError(error as Error);
} else {
report(error);
warn('Cannot provide completion', error);
}
}

Expand Down
20 changes: 12 additions & 8 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,29 @@ const YELLOW = '\x1b[93m';
const RESET = '\x1b[0m';
const BOLD = '\x1b[1m';

export const report = (error: unknown): {message: string; stack?: string} => {
let errorMessage: string;

const parseErrorMessage = (error: unknown): string => {
if (error instanceof Error) {
errorMessage = error.message;
return error.message;
} else if (typeof error === 'string') {
errorMessage = error;
return error;
} else {
errorMessage = 'An unknown error occurred';
return 'An unknown error occurred';
}
};

export const report = (error: unknown): {message: string; stack?: string} => {
const errorMessage = parseErrorMessage(error);

const formattedError = `${RED}${BOLD}[MONACOPILOT ERROR] ${errorMessage}${RESET}`;
console.error(formattedError);

return {message: errorMessage};
};

export const warn = (message: string): void => {
console.warn(`${YELLOW}${BOLD}[MONACOPILOT WARN] ${message}${RESET}`);
export const warn = (message: string, error?: unknown): void => {
console.warn(
`${YELLOW}${BOLD}[MONACOPILOT WARN] ${message}${error ? `\n${parseErrorMessage(error)}` : ''}${RESET}`,
);
};

export const log = (message: string): void => {
Expand Down
7 changes: 0 additions & 7 deletions tests/ui/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,6 @@ export default function Home() {
trigger: 'onTyping',
});

editor.addCommand(
monaco.KeyMod.CtrlCmd | monaco.KeyMod.Shift | monaco.KeyCode.Space,
() => {
completion.trigger();
},
);

return () => {
completion.deregister();
};
Expand Down

0 comments on commit 17fefee

Please sign in to comment.