Skip to content

Commit

Permalink
Fix the incorrect error message from the CLI.
Browse files Browse the repository at this point in the history
Signed-off-by: Akos Kitta <[email protected]>
  • Loading branch information
Akos Kitta committed Jul 14, 2022
1 parent 95971a2 commit 350c04b
Showing 1 changed file with 41 additions and 4 deletions.
45 changes: 41 additions & 4 deletions arduino-ide-extension/src/node/sketches-service-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { promisify } from 'util';
import URI from '@theia/core/lib/common/uri';
import { FileUri } from '@theia/core/lib/node';
import { isWindows, isOSX } from '@theia/core/lib/common/os';
import { ConfigService } from '../common/protocol/config-service';
import { ConfigServiceImpl } from './config-service-impl';
import {
SketchesService,
Sketch,
Expand Down Expand Up @@ -50,8 +50,8 @@ export class SketchesServiceImpl
? tempDir
: maybeNormalizeDrive(fs.realpathSync.native(tempDir));

@inject(ConfigService)
protected readonly configService: ConfigService;
@inject(ConfigServiceImpl)
protected readonly configService: ConfigServiceImpl;

@inject(NotificationServiceServerImpl)
protected readonly notificationService: NotificationServiceServerImpl;
Expand Down Expand Up @@ -205,7 +205,14 @@ export class SketchesServiceImpl
if (err) {
reject(
isNotFoundError(err)
? SketchesError.NotFound(err.details, uri)
? SketchesError.NotFound(
fixErrorMessage(
err,
requestSketchPath,
this.configService.cliConfiguration?.directories.user
),
uri
)
: err
);
return;
Expand Down Expand Up @@ -583,6 +590,36 @@ interface SketchWithDetails extends Sketch {
readonly mtimeMs: number;
}

// https://github.com/arduino/arduino-cli/issues/1797
function fixErrorMessage(
err: ServiceError,
sketchPath: string,
sketchbookPath: string | undefined
): string {
if (!sketchbookPath) {
return err.details; // No way to repair the error message. The current sketchbook path is not available.
}
// Original: `Can't open sketch: no valid sketch found in /Users/a.kitta/Documents/Arduino: missing /Users/a.kitta/Documents/Arduino/Arduino.ino`
// Fixed: `Can't open sketch: no valid sketch found in /Users/a.kitta/Documents/Arduino: missing $sketchPath`
const message = err.details;
const incorrectMessageSuffix = path.join(sketchbookPath, 'Arduino.ino');
if (
message.startsWith("Can't open sketch: no valid sketch found in") &&
message.endsWith(`${incorrectMessageSuffix}`)
) {
const sketchName = path.basename(sketchPath);
const correctMessagePrefix = message.substring(
0,
message.length - incorrectMessageSuffix.length
);
return `${correctMessagePrefix}${path.join(
sketchPath,
`${sketchName}.ino`
)}`;
}
return err.details;
}

function isNotFoundError(err: unknown): err is ServiceError {
return ServiceError.is(err) && err.code === 5; // `NOT_FOUND` https://grpc.github.io/grpc/core/md_doc_statuscodes.html
}
Expand Down

0 comments on commit 350c04b

Please sign in to comment.