Skip to content

Commit 1aeecf8

Browse files
committed
Added error handling for more helpful messages to the user
1 parent 2d826f4 commit 1aeecf8

File tree

2 files changed

+42
-11
lines changed

2 files changed

+42
-11
lines changed

Diff for: handler/builder.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -141,18 +141,21 @@ func logCommandErr(command string, stdout []byte, err error, filter func(string)
141141
log.Println("Command error:", command, err)
142142
if len(stdout) > 0 {
143143
stdoutStr := string(stdout)
144-
log.Println("------------------------------BEGIN STDOUT\n", stdoutStr, "\n------------------------------END STDOUT")
144+
log.Println("------------------------------BEGIN STDOUT\n", stdoutStr, "------------------------------END STDOUT")
145145
message += filter(stdoutStr)
146146
}
147147
if exitErr, ok := err.(*exec.ExitError); ok {
148148
stderr := exitErr.Stderr
149149
if len(stderr) > 0 {
150150
stderrStr := string(stderr)
151-
log.Println("------------------------------BEGIN STDERR\n", stderrStr, "\n------------------------------END STDERR")
151+
log.Println("------------------------------BEGIN STDERR\n", stderrStr, "------------------------------END STDERR")
152152
message += filter(stderrStr)
153153
}
154154
}
155-
return errors.Wrap(err, message)
155+
if len(message) == 0 {
156+
return err
157+
}
158+
return errors.New(message)
156159
}
157160

158161
func errMsgFilter(tempDir string) func(string) string {

Diff for: handler/handler.go

+36-8
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ import (
88
"log"
99
"net/url"
1010
"path/filepath"
11+
"regexp"
1112
"strings"
1213

14+
"github.com/pkg/errors"
1315
lsp "github.com/sourcegraph/go-lsp"
1416
"github.com/sourcegraph/jsonrpc2"
1517
)
@@ -162,8 +164,7 @@ func (handler *InoHandler) createFileData(ctx context.Context, sourceURI lsp.Doc
162164
fqbn := "arduino:avr:uno"
163165
targetPath, targetBytes, err := generateCpp([]byte(sourceText), filepath.Base(sourcePath), fqbn)
164166
if err != nil {
165-
message := "Could not start editor support:\n" + err.Error()
166-
go handler.showMessage(ctx, lsp.MTError, message)
167+
handler.handleError(ctx, err, fqbn)
167168
return nil, nil, err
168169
}
169170

@@ -225,6 +226,23 @@ func (handler *InoHandler) deleteFileData(sourceURI lsp.DocumentURI) {
225226
}
226227
}
227228

229+
func (handler *InoHandler) handleError(ctx context.Context, err error, fqbn string) {
230+
errorStr := err.Error()
231+
var message string
232+
if strings.Contains(errorStr, "platform not installed") {
233+
message = "Editor support is disabled because the platform `" + fqbn + "` is not installed."
234+
message += " Use the Boards Manager to install it."
235+
} else if strings.Contains(errorStr, "No such file or directory") {
236+
exp, _ := regexp.Compile("([\\w\\.\\-]+)\\.h: No such file or directory")
237+
submatch := exp.FindStringSubmatch(errorStr)
238+
message = "Editor support is disabled because the library `" + submatch[1] + "` is not installed."
239+
message += " Use the Library Manager to install it"
240+
} else {
241+
message = "Could not start editor support.\n" + errorStr
242+
}
243+
go handler.showMessage(ctx, lsp.MTError, message)
244+
}
245+
228246
func (handler *InoHandler) ino2cppTextDocumentIdentifier(doc *lsp.TextDocumentIdentifier) error {
229247
if data, ok := handler.data[doc.URI]; ok {
230248
doc.URI = data.targetURI
@@ -254,17 +272,19 @@ func (handler *InoHandler) ino2cppDidChangeTextDocumentParams(params *lsp.DidCha
254272
return err
255273
}
256274
}
275+
return nil
257276
}
258-
return nil
277+
return unknownURI(params.TextDocument.URI)
259278
}
260279

261280
func (handler *InoHandler) ino2cppTextDocumentPositionParams(params *lsp.TextDocumentPositionParams) error {
262281
handler.ino2cppTextDocumentIdentifier(&params.TextDocument)
263282
if data, ok := handler.data[params.TextDocument.URI]; ok {
264283
targetLine := data.targetLineMap[params.Position.Line]
265284
params.Position.Line = targetLine
285+
return nil
266286
}
267-
return nil
287+
return unknownURI(params.TextDocument.URI)
268288
}
269289

270290
func (handler *InoHandler) ino2cppCodeActionParams(params *lsp.CodeActionParams) error {
@@ -277,33 +297,37 @@ func (handler *InoHandler) ino2cppCodeActionParams(params *lsp.CodeActionParams)
277297
r.Start.Line = data.targetLineMap[r.Start.Line]
278298
r.End.Line = data.targetLineMap[r.End.Line]
279299
}
300+
return nil
280301
}
281-
return nil
302+
return unknownURI(params.TextDocument.URI)
282303
}
283304

284305
func (handler *InoHandler) ino2cppDocumentRangeFormattingParams(params *lsp.DocumentRangeFormattingParams) error {
285306
handler.ino2cppTextDocumentIdentifier(&params.TextDocument)
286307
if data, ok := handler.data[params.TextDocument.URI]; ok {
287308
params.Range.Start.Line = data.targetLineMap[params.Range.Start.Line]
288309
params.Range.End.Line = data.targetLineMap[params.Range.End.Line]
310+
return nil
289311
}
290-
return nil
312+
return unknownURI(params.TextDocument.URI)
291313
}
292314

293315
func (handler *InoHandler) ino2cppDocumentOnTypeFormattingParams(params *lsp.DocumentOnTypeFormattingParams) error {
294316
handler.ino2cppTextDocumentIdentifier(&params.TextDocument)
295317
if data, ok := handler.data[params.TextDocument.URI]; ok {
296318
params.Position.Line = data.targetLineMap[params.Position.Line]
319+
return nil
297320
}
298-
return nil
321+
return unknownURI(params.TextDocument.URI)
299322
}
300323

301324
func (handler *InoHandler) ino2cppRenameParams(params *lsp.RenameParams) error {
302325
handler.ino2cppTextDocumentIdentifier(&params.TextDocument)
303326
if data, ok := handler.data[params.TextDocument.URI]; ok {
304327
params.Position.Line = data.targetLineMap[params.Position.Line]
328+
return nil
305329
}
306-
return nil
330+
return unknownURI(params.TextDocument.URI)
307331
}
308332

309333
func (handler *InoHandler) transformClangdResult(method string, uri lsp.DocumentURI, result interface{}) interface{} {
@@ -538,3 +562,7 @@ func uriToPath(uri lsp.DocumentURI) string {
538562
func pathToURI(path string) lsp.DocumentURI {
539563
return "file://" + lsp.DocumentURI(filepath.ToSlash(path))
540564
}
565+
566+
func unknownURI(uri lsp.DocumentURI) error {
567+
return errors.New("Document is not available: " + string(uri))
568+
}

0 commit comments

Comments
 (0)