Skip to content

Commit c186cd0

Browse files
committed
Fixed file modification calculator handling of an empty file '[]'. Added tests for auto-import
1 parent f40e04c commit c186cd0

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

src/api/dashboard/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { config } from '../../config.js';
22
import { LoginHelper } from '../../connect/login-helper.js';
33
import { CloudDocument } from './types.js';
4+
import { UnauthorizedError } from '../../common/errors.js';
45

56
export const DashboardApiClient = {
67
async getDocument(id: string): Promise<CloudDocument> {
@@ -35,7 +36,13 @@ export const DashboardApiClient = {
3536
);
3637

3738
if (!res.ok) {
39+
console.log('Request not okay', res.status)
3840
const message = await res.text();
41+
if (res.status === 401) {
42+
console.log('Unauthorized');
43+
throw new UnauthorizedError({ requestName: 'getDefaultDocumentId' });
44+
}
45+
3946
throw new Error(message);
4047
}
4148

src/common/errors.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,20 @@ export class SyntaxError extends CodifyError {
142142
}
143143
}
144144

145+
export class UnauthorizedError extends CodifyError {
146+
name = 'UnauthorizedError'
147+
requestName?: string
148+
149+
constructor(props: Omit<RemoveErrorMethods<UnauthorizedError>, 'message'>) {
150+
super(`Unauthorized request to Codify. ${props.requestName ?? ''}`)
151+
Object.assign(this, props);
152+
}
153+
154+
formattedMessage(): string {
155+
return this.message
156+
}
157+
}
158+
145159
export function prettyPrintError(error: unknown): void {
146160
if (error instanceof CodifyError) {
147161
return console.error(chalk.red(error.formattedMessage()));

src/common/initialize-plugins.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export class PluginInitOrchestrator {
107107
}
108108

109109
if (LoginHelper.get()?.isLoggedIn) {
110-
return DashboardApiClient.getDefaultDocumentId();
110+
return (await DashboardApiClient.getDefaultDocumentId()) ?? undefined;
111111
}
112112

113113
if (args.allowEmptyProject) {

src/orchestrators/edit.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Config } from '@oclif/core';
2-
import { randomBytes } from 'node:crypto';
32
import open from 'open';
43

54
import { DashboardApiClient } from '../api/dashboard/index.js';
@@ -9,7 +8,6 @@ import { ConnectOrchestrator } from './connect.js';
98
import { LoginOrchestrator } from './login.js';
109

1110
export class EditOrchestrator {
12-
static rootCommand: string;
1311

1412
static async run(oclifConfig: Config) {
1513
const login = LoginHelper.get()?.isLoggedIn;
@@ -18,7 +16,13 @@ export class EditOrchestrator {
1816
await LoginOrchestrator.run();
1917
}
2018

21-
const defaultDocumentId = await DashboardApiClient.getDefaultDocumentId();
19+
let defaultDocumentId: null | string = null;
20+
try {
21+
defaultDocumentId = await DashboardApiClient.getDefaultDocumentId();
22+
} catch {
23+
console.warn('Mismatch accounts between local and dashboard. Cannot open default document')
24+
}
25+
2226
const url = defaultDocumentId
2327
? `${config.dashboardUrl}/file/${defaultDocumentId}`
2428
: config.dashboardUrl;

0 commit comments

Comments
 (0)