Skip to content

Commit 70239cb

Browse files
committed
move error logging from common to npm app
1 parent e9bc924 commit 70239cb

File tree

2 files changed

+35
-28
lines changed

2 files changed

+35
-28
lines changed

common/src/websockets/websocket-client.ts

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -228,27 +228,9 @@ export class APIRealtimeClient {
228228
}
229229

230230
async sendAction(action: ClientAction) {
231-
try {
232-
return await this.sendMessage('action', {
233-
data: action,
234-
})
235-
} catch (e) {
236-
// Print the error message for debugging.
237-
console.error(
238-
'Error sending action:',
239-
action.type,
240-
typeof e === 'object' && e !== null && 'message' in e ? e.message : e,
241-
)
242-
243-
console.log()
244-
console.log('Codebuff is exiting due to an error.')
245-
console.log('Make sure you are on the latest version of Codebuff!')
246-
console.log('-----------------------------------')
247-
console.log('Please run: npm install -g codebuff')
248-
console.log('-----------------------------------')
249-
250-
process.exit(1)
251-
}
231+
return await this.sendMessage('action', {
232+
data: action,
233+
})
252234
}
253235

254236
subscribe<T extends ServerAction['type']>(

npm-app/src/client.ts

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,31 @@ import type { ProjectFileContext } from '@codebuff/common/util/file'
107107

108108
const LOW_BALANCE_THRESHOLD = 100
109109

110+
async function sendActionAndHandleError(
111+
ws: APIRealtimeClient,
112+
action: ClientAction,
113+
) {
114+
try {
115+
return await ws.sendAction(action)
116+
} catch (e) {
117+
// Print the error message for debugging.
118+
console.error(
119+
'Error sending action:',
120+
action.type,
121+
typeof e === 'object' && e !== null && 'message' in e ? e.message : e,
122+
)
123+
124+
console.log()
125+
console.log('Codebuff is exiting due to an error.')
126+
console.log('Make sure you are on the latest version of Codebuff!')
127+
console.log('-----------------------------------')
128+
console.log('Please run: npm install -g codebuff')
129+
console.log('-----------------------------------')
130+
131+
process.exit(1)
132+
}
133+
}
134+
110135
const WARNING_CONFIG = {
111136
[UserState.LOGGED_OUT]: {
112137
message: () => `Type "login" to unlock full access and get free credits!`,
@@ -750,7 +775,7 @@ export class Client {
750775
const { filePaths, requestId } = a
751776
const files = getFiles(filePaths)
752777

753-
this.webSocket.sendAction({
778+
sendActionAndHandleError(this.webSocket, {
754779
type: 'read-files-response',
755780
files,
756781
requestId,
@@ -778,7 +803,7 @@ export class Client {
778803
'User input ID mismatch - rejecting tool call request',
779804
)
780805

781-
this.webSocket.sendAction({
806+
sendActionAndHandleError(this.webSocket, {
782807
type: 'tool-call-response',
783808
requestId,
784809
success: false,
@@ -804,7 +829,7 @@ export class Client {
804829
if (this.userInputId) {
805830
Spinner.get().start('Processing results...')
806831
}
807-
this.webSocket.sendAction({
832+
sendActionAndHandleError(this.webSocket, {
808833
type: 'tool-call-response',
809834
requestId,
810835
success: true,
@@ -823,7 +848,7 @@ export class Client {
823848

824849
// Send error response back to backend
825850
Spinner.get().start('Fixing...')
826-
this.webSocket.sendAction({
851+
sendActionAndHandleError(this.webSocket, {
827852
type: 'tool-call-response',
828853
requestId,
829854
success: false,
@@ -1032,7 +1057,7 @@ export class Client {
10321057
repoUrl: loggerContext.repoUrl,
10331058
// repoName: loggerContext.repoName,
10341059
}
1035-
this.webSocket.sendAction(action)
1060+
sendActionAndHandleError(this.webSocket, action)
10361061

10371062
return {
10381063
responsePromise,
@@ -1116,7 +1141,7 @@ export class Client {
11161141
(id) => id !== this.userInputId,
11171142
)
11181143

1119-
this.webSocket.sendAction({
1144+
sendActionAndHandleError(this.webSocket, {
11201145
type: 'cancel-user-input',
11211146
authToken: this.user?.authToken,
11221147
promptId: this.userInputId,
@@ -1554,7 +1579,7 @@ Go to https://www.codebuff.com/config for more information.`) +
15541579
// Add repoUrl here as per the diff for client.ts
15551580
repoUrl: loggerContext.repoUrl,
15561581
}
1557-
this.webSocket.sendAction(initAction)
1582+
sendActionAndHandleError(this.webSocket, initAction)
15581583

15591584
await this.fetchStoredApiKeyTypes()
15601585
}

0 commit comments

Comments
 (0)