Getting errors from Monaco editor
Asked Answered
C

3

6

I would like to get hold of the errors created by default by Monaco editor.

enter image description here

Cabbala answered 18/4, 2017 at 4:0 Comment(0)
H
7

It looks like you can call monaco.editor.getModelMarkers({}) to get the list of all markers in the document, and then filter that yourself to limit it to the errors you're interested in. I would prefer a more clearly-documented route, but in my ad-hoc testing this works.

Hawkins answered 6/9, 2017 at 2:32 Comment(0)
G
4

Given a monaco model, you can have access to the worker. This is similar to a ts.LanguageService but the signatures are async. With that you can then call getSemanticDiagnostics() and all the rest.

monaco.languages.typescript.getTypeScriptWorker()
  .then(_worker=>{_worker(model.uri)
  .then(worker=>{

    worker.getScriptFileNames().then(ff=>{
      ff.forEach(sf=>{
        worker.getSemanticDiagnostics(sf).then(dd=>{          
          console.log('\n\n DIAGNOSTICS FOR '+sf)
          console.log(dd.map(d=>d.messageText))})
        })
Gatlin answered 26/6, 2018 at 21:7 Comment(0)
D
4

This is a simple sample to log errors:

import * as monaco from 'monaco-editor'

// ...

monaco.editor.onDidChangeMarkers(([uri]) => {
  const markers = monaco.editor.getModelMarkers({resource: uri})
  console.log('markers:', markers.map(
    ({ message, startLineNumber, startColumn, endLineNumber, endColumn }) =>
      `${message} [${startLineNumber}:${startColumn}-${endLineNumber}:${endColumn}]`,
  ))
})
Depalma answered 12/2, 2022 at 20:45 Comment(1)
As of Oct 2022 this works with @monaco-editor/react, but TS is NOT happy with it.Popovich

© 2022 - 2024 — McMap. All rights reserved.