Skip to content

Commit 0e39f9e

Browse files
author
Akos Kitta
committed
fix: better create error types
Signed-off-by: Akos Kitta <[email protected]>
1 parent 4adf008 commit 0e39f9e

File tree

3 files changed

+32
-19
lines changed

3 files changed

+32
-19
lines changed

arduino-ide-extension/src/browser/contributions/create-contribution.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { CompositeTreeNode } from '@theia/core/lib/browser';
12
import URI from '@theia/core/lib/common/uri';
23
import { inject, injectable } from '@theia/core/shared/inversify';
34
import { Sketch } from '../../common/protocol';
@@ -25,19 +26,26 @@ export abstract class CloudSketchContribution extends SketchContribution {
2526
return this.localCacheFsProvider.from(new URI(sketch.uri));
2627
}
2728

28-
protected async treeModel(): Promise<CloudSketchbookTreeModel | undefined> {
29+
protected async treeModel(): Promise<
30+
(CloudSketchbookTreeModel & { root: CompositeTreeNode }) | undefined
31+
> {
2932
const { enabled, session } = this.createFeatures;
3033
if (enabled && session) {
3134
const widget = await this.widgetContribution.widget;
3235
const treeModel = this.treeModelFrom(widget);
3336
if (treeModel) {
34-
return treeModel;
37+
const root = treeModel.root;
38+
if (CompositeTreeNode.is(root)) {
39+
return treeModel as CloudSketchbookTreeModel & {
40+
root: CompositeTreeNode;
41+
};
42+
}
3543
}
3644
}
3745
return undefined;
3846
}
3947

40-
treeModelFrom(
48+
private treeModelFrom(
4149
widget: SketchbookWidget
4250
): CloudSketchbookTreeModel | undefined {
4351
for (const treeWidget of widget.getTreeWidgets()) {

arduino-ide-extension/src/browser/contributions/new-cloud-sketch.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ import { injectable } from '@theia/core/shared/inversify';
1818
import { WorkspaceInputDialogProps } from '@theia/workspace/lib/browser/workspace-input-dialog';
1919
import { v4 } from 'uuid';
2020
import { CreateUri } from '../create/create-uri';
21-
import { Create, isConflict, isNotFound } from '../create/typings';
21+
import {
22+
ConflictError,
23+
Create,
24+
CreateError,
25+
isConflict,
26+
isNotFound,
27+
} from '../create/typings';
2228
import { ArduinoMenus } from '../menu/arduino-menus';
2329
import { WorkspaceInputDialog } from '../theia/workspace/workspace-input-dialog';
2430
import { CloudSketchbookTree } from '../widgets/cloud-sketchbook/cloud-sketchbook-tree';
@@ -72,24 +78,19 @@ export class NewCloudSketch extends CloudSketchContribution {
7278
initialValue?: string | undefined
7379
): Promise<unknown> {
7480
const treeModel = await this.treeModel();
75-
if (!treeModel) {
76-
return undefined;
77-
}
78-
const rootNode = CompositeTreeNode.is(treeModel.root)
79-
? treeModel.root
80-
: undefined;
81-
if (!rootNode) {
82-
return undefined;
81+
if (treeModel) {
82+
const rootNode = treeModel.root;
83+
return this.openWizard(rootNode, treeModel, initialValue);
8384
}
84-
return this.openWizard(rootNode, treeModel, initialValue);
85+
return undefined;
8586
}
8687

8788
private createNewWithProgress(
8889
value: string,
8990
treeModel: CloudSketchbookTreeModel
9091
): (progress: Progress) => Promise<unknown> {
9192
return async (progress: Progress) => {
92-
let result: Create.Sketch | undefined | 'conflict';
93+
let result: Create.Sketch | undefined | ConflictError;
9394
try {
9495
progress.report({
9596
message: nls.localize(
@@ -101,7 +102,7 @@ export class NewCloudSketch extends CloudSketchContribution {
101102
result = await this.createApi.createSketch(value);
102103
} catch (err) {
103104
if (isConflict(err)) {
104-
result = 'conflict';
105+
result = err;
105106
} else {
106107
throw err;
107108
}
@@ -117,7 +118,7 @@ export class NewCloudSketch extends CloudSketchContribution {
117118
await treeModel.refresh();
118119
}
119120
}
120-
if (result === 'conflict') {
121+
if (result instanceof CreateError) {
121122
return this.createNewSketch(value);
122123
}
123124
if (result) {

arduino-ide-extension/src/browser/create/typings.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,20 @@ export class CreateError extends Error {
7272
}
7373
}
7474

75-
export function isConflict(err: unknown): boolean {
75+
export type ConflictError = CreateError & { status: 409 };
76+
export function isConflict(err: unknown): err is ConflictError {
7677
return isErrorWithStatusOf(err, 409);
7778
}
78-
export function isNotFound(err: unknown): boolean {
79+
80+
export type NotFoundError = CreateError & { status: 404 };
81+
export function isNotFound(err: unknown): err is NotFoundError {
7982
return isErrorWithStatusOf(err, 404);
8083
}
84+
8185
function isErrorWithStatusOf(
8286
err: unknown,
8387
status: number
84-
): err is Error & { status: number } {
88+
): err is CreateError & { status: number } {
8589
if (err instanceof CreateError) {
8690
return err.status === status;
8791
}

0 commit comments

Comments
 (0)