Skip to content

Commit 53224fc

Browse files
authored
refactor(vscode): centralise error handlers (#4679)
1 parent 906ed7a commit 53224fc

File tree

3 files changed

+42
-75
lines changed

3 files changed

+42
-75
lines changed

vscode/extension/src/commands/format.ts

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,7 @@ import { traceLog } from '../utilities/common/log'
22
import { sqlmeshExec } from '../utilities/sqlmesh/sqlmesh'
33
import { err, isErr, ok, Result } from '@bus/result'
44
import * as vscode from 'vscode'
5-
import {
6-
ErrorType,
7-
handleNotSginedInError,
8-
handleSqlmeshLspNotFoundError,
9-
handleSqlmeshLspDependenciesMissingError,
10-
handleTcloudBinNotFoundError,
11-
} from '../utilities/errors'
5+
import { ErrorType, handleError } from '../utilities/errors'
126
import { AuthenticationProviderTobikoCloud } from '../auth/auth'
137
import { execAsync } from '../utilities/exec'
148

@@ -18,25 +12,7 @@ export const format =
1812
traceLog('Calling format')
1913
const out = await internalFormat()
2014
if (isErr(out)) {
21-
switch (out.error.type) {
22-
case 'not_signed_in':
23-
await handleNotSginedInError(authProvider)
24-
return
25-
case 'sqlmesh_lsp_not_found':
26-
await handleSqlmeshLspNotFoundError()
27-
return
28-
case 'sqlmesh_lsp_dependencies_missing':
29-
await handleSqlmeshLspDependenciesMissingError(out.error)
30-
return
31-
case 'tcloud_bin_not_found':
32-
await handleTcloudBinNotFoundError()
33-
return
34-
case 'generic':
35-
await vscode.window.showErrorMessage(
36-
`Project format failed: ${out.error.message}`,
37-
)
38-
return
39-
}
15+
return handleError(authProvider, out.error, 'Project format failed')
4016
}
4117
vscode.window.showInformationMessage('Project formatted successfully')
4218
}

vscode/extension/src/extension.ts

Lines changed: 7 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,7 @@ import { signIn } from './commands/signin'
1414
import { signInSpecifyFlow } from './commands/signinSpecifyFlow'
1515
import { renderModel, reRenderModelForSourceFile } from './commands/renderModel'
1616
import { isErr } from '@bus/result'
17-
import {
18-
handleNotSginedInError,
19-
handleSqlmeshLspNotFoundError,
20-
handleSqlmeshLspDependenciesMissingError,
21-
handleTcloudBinNotFoundError,
22-
} from './utilities/errors'
17+
import { handleError } from './utilities/errors'
2318
import { selector, completionProvider } from './completion/completion'
2419
import { LineagePanel } from './webviews/lineagePanel'
2520
import { RenderedModelProvider } from './providers/renderedModelProvider'
@@ -117,25 +112,11 @@ export async function activate(context: vscode.ExtensionContext) {
117112
traceVerbose('Restarting LSP client')
118113
const restartResult = await lspClient.restart()
119114
if (isErr(restartResult)) {
120-
switch (restartResult.error.type) {
121-
case 'not_signed_in':
122-
await handleNotSginedInError(authProvider)
123-
return
124-
case 'sqlmesh_lsp_not_found':
125-
await handleSqlmeshLspNotFoundError()
126-
return
127-
case 'sqlmesh_lsp_dependencies_missing':
128-
await handleSqlmeshLspDependenciesMissingError(restartResult.error)
129-
return
130-
case 'tcloud_bin_not_found':
131-
await handleTcloudBinNotFoundError()
132-
return
133-
case 'generic':
134-
await vscode.window.showErrorMessage(
135-
`Failed to restart LSP: ${restartResult.error.message}`,
136-
)
137-
return
138-
}
115+
return handleError(
116+
authProvider,
117+
restartResult.error,
118+
'LSP restart failed',
119+
)
139120
}
140121
context.subscriptions.push(lspClient)
141122
}
@@ -155,25 +136,7 @@ export async function activate(context: vscode.ExtensionContext) {
155136

156137
const result = await lspClient.start()
157138
if (isErr(result)) {
158-
switch (result.error.type) {
159-
case 'not_signed_in':
160-
await handleNotSginedInError(authProvider)
161-
break
162-
case 'sqlmesh_lsp_not_found':
163-
await handleSqlmeshLspNotFoundError()
164-
break
165-
case 'sqlmesh_lsp_dependencies_missing':
166-
await handleSqlmeshLspDependenciesMissingError(result.error)
167-
break
168-
case 'tcloud_bin_not_found':
169-
await handleTcloudBinNotFoundError()
170-
break
171-
case 'generic':
172-
await vscode.window.showErrorMessage(
173-
`Failed to start LSP: ${result.error.message}`,
174-
)
175-
break
176-
}
139+
return handleError(authProvider, result.error, 'Failed to start LSP')
177140
} else {
178141
context.subscriptions.push(lspClient)
179142
}

vscode/extension/src/utilities/errors.ts

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,49 @@ export type ErrorType =
1313
// tcloud_bin_not_found is used when the tcloud executable is not found. This is likely to happen if the user
1414
// opens a project that has a `tcloud.yaml` file but doesn't have tcloud installed.
1515
| { type: 'tcloud_bin_not_found' }
16-
// sqlmesh_lsp_dependencies_missing is used when the sqlmesh_lsp is found but the lsp extras are missing.
1716
| SqlmeshLspDependenciesMissingError
1817

18+
/**
19+
* SqlmeshLspDependenciesMissingError is used when the sqlmesh_lsp is found but
20+
* the lsp extras are missing.
21+
*/
1922
interface SqlmeshLspDependenciesMissingError {
2023
type: 'sqlmesh_lsp_dependencies_missing'
2124
is_missing_pygls: boolean
2225
is_missing_lsprotocol: boolean
2326
is_tobiko_cloud: boolean
2427
}
2528

29+
export async function handleError(
30+
authProvider: AuthenticationProviderTobikoCloud,
31+
error: ErrorType,
32+
genericErrorPrefix?: string,
33+
): Promise<void> {
34+
traceInfo('handleError', error)
35+
switch (error.type) {
36+
case 'not_signed_in':
37+
return handleNotSignedInError(authProvider)
38+
case 'sqlmesh_lsp_not_found':
39+
return handleSqlmeshLspNotFoundError()
40+
case 'sqlmesh_lsp_dependencies_missing':
41+
return handleSqlmeshLspDependenciesMissingError(error)
42+
case 'tcloud_bin_not_found':
43+
return handleTcloudBinNotFoundError()
44+
case 'generic':
45+
if (genericErrorPrefix) {
46+
await window.showErrorMessage(`${genericErrorPrefix}: ${error.message}`)
47+
} else {
48+
await window.showErrorMessage(`An error occurred: ${error.message}`)
49+
}
50+
return
51+
}
52+
}
53+
2654
/**
2755
* Handles the case where the user is not signed in to Tobiko Cloud.
2856
* @param authProvider - The authentication provider to use for signing in.
2957
*/
30-
export const handleNotSginedInError = async (
58+
const handleNotSignedInError = async (
3159
authProvider: AuthenticationProviderTobikoCloud,
3260
): Promise<void> => {
3361
traceInfo('handleNotSginedInError')
@@ -43,7 +71,7 @@ export const handleNotSginedInError = async (
4371
/**
4472
* Handles the case where the sqlmesh_lsp is not found.
4573
*/
46-
export const handleSqlmeshLspNotFoundError = async (): Promise<void> => {
74+
const handleSqlmeshLspNotFoundError = async (): Promise<void> => {
4775
traceInfo('handleSqlmeshLspNotFoundError')
4876
await window.showErrorMessage(
4977
'SQLMesh LSP not found, please check installation',
@@ -53,7 +81,7 @@ export const handleSqlmeshLspNotFoundError = async (): Promise<void> => {
5381
/**
5482
* Handles the case where the sqlmesh_lsp is found but the lsp extras are missing.
5583
*/
56-
export const handleSqlmeshLspDependenciesMissingError = async (
84+
const handleSqlmeshLspDependenciesMissingError = async (
5785
error: SqlmeshLspDependenciesMissingError,
5886
): Promise<void> => {
5987
traceInfo('handleSqlmeshLspDependenciesMissingError')
@@ -80,7 +108,7 @@ export const handleSqlmeshLspDependenciesMissingError = async (
80108
/**
81109
* Handles the case where the tcloud executable is not found.
82110
*/
83-
export const handleTcloudBinNotFoundError = async (): Promise<void> => {
111+
const handleTcloudBinNotFoundError = async (): Promise<void> => {
84112
const result = await window.showErrorMessage(
85113
'tcloud executable not found, please check installation',
86114
'Install',

0 commit comments

Comments
 (0)