TypeFox / monaco-languageclient

Repo hosts npm packages for monaco-languageclient, vscode-ws-jsonrpc, monaco-editor-wrapper, @typefox/monaco-editor-react and monaco-languageclient-examples
https://www.npmjs.com/package/monaco-languageclient
MIT License
1.06k stars 177 forks source link

How to obtain backend error messages, And that are not displayed in the console? #766

Open evilpose opened 4 days ago

evilpose commented 4 days ago

I want to be able to proactively capture this error, so that it is not displayed in the console, but in the form of a UI on the page. I couldn't find a similar hook, can you help me check this problem?

image image

export const createLanguageClient = (
  transports: MessageTransports,
): MonacoLanguageClient => {
  return new MonacoLanguageClient({
    name: 'xxx Language Client',
    clientOptions: {
      documentSelector: ['isogql'],
      errorHandler: {
        error: () => ({ action: ErrorAction.Continue }),
        closed: () => ({ action: CloseAction.Restart }),
      },
    },
    connectionProvider: {
      get: () => {
        return Promise.resolve(transports);
      },
    },
  });
};

export const createWebSocket = (url: string): WebSocket => {
  const webSocket = new WebSocket(url);
  webSocket.onopen = () => {
    const socket = toSocket(webSocket);
    const reader = new WebSocketMessageReader(socket);
    const writer = new WebSocketMessageWriter(socket);
    const languageClient = createLanguageClient({
      reader,
      writer,
    });
    languageClient.start();
    reader.onClose(() => {
      createWebSocket(url);
    });
  };
  return webSocket;
};
CGNonofr commented 4 days ago

You can probably replace the Log/Logger service override with you custom impl. The default one logs to the console

CGNonofr commented 4 days ago

I've answered too fast! It's an UnexpectedError, the code allows to override the handler but the libs don't expose it, fixing that

CGNonofr commented 4 days ago

with https://github.com/CodinGame/monaco-vscode-api/pull/513, you'll be able to do

import { setUnexpectedErrorHandler } from 'vscode/monaco'

setUnexpectedErrorHandler((e) => {
  // whatever you want
})