Skip to content

Commit fac01f8

Browse files
committed
unify errorToObject and getErrorObject
1 parent 635678c commit fac01f8

File tree

7 files changed

+62
-33
lines changed

7 files changed

+62
-33
lines changed

backend/src/llm-apis/vercel-ai-sdk/ai-sdk.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import {
55
openaiModels,
66
} from '@codebuff/common/old-constants'
77
import { buildArray } from '@codebuff/common/util/array'
8+
import { getErrorObject } from '@codebuff/common/util/error'
89
import { convertCbToModelMessages } from '@codebuff/common/util/messages'
9-
import { errorToObject } from '@codebuff/common/util/object'
1010
import { withTimeout } from '@codebuff/common/util/promise'
1111
import { StopSequenceHandler } from '@codebuff/common/util/stop-sequence'
1212
import { generateCompactId } from '@codebuff/common/util/string'
@@ -121,7 +121,7 @@ export const promptAiSdkStream = async function* (
121121
logger.error(
122122
{
123123
chunk: { ...chunk, error: undefined },
124-
error: errorToObject(chunk.error),
124+
error: getErrorObject(chunk.error),
125125
model: options.model,
126126
},
127127
'Error from AI SDK',

backend/src/util/messages.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { AssertionError } from 'assert'
22

33
import { buildArray } from '@codebuff/common/util/array'
4-
import { errorToObject } from '@codebuff/common/util/object'
4+
import { getErrorObject } from '@codebuff/common/util/error'
55
import { closeXml } from '@codebuff/common/util/xml'
66
import { cloneDeep, isEqual } from 'lodash'
77

@@ -283,7 +283,7 @@ export function getEditedFiles(messages: Message[]): string[] {
283283
return fileInfo.file
284284
} catch (error) {
285285
logger.error(
286-
{ error: errorToObject(error), m },
286+
{ error: getErrorObject(error), m },
287287
'Error parsing file info',
288288
)
289289
return null
@@ -314,7 +314,7 @@ export function getPreviouslyReadFiles(messages: Message[]): {
314314
)
315315
} catch (error) {
316316
logger.error(
317-
{ error: errorToObject(error), message },
317+
{ error: getErrorObject(error), message },
318318
'Error parsing read_files output from message',
319319
)
320320
}
@@ -337,7 +337,7 @@ export function getPreviouslyReadFiles(messages: Message[]): {
337337
)
338338
} catch (error) {
339339
logger.error(
340-
{ error: errorToObject(error), message },
340+
{ error: getErrorObject(error), message },
341341
'Error parsing find_files output from message',
342342
)
343343
}

backend/src/util/simplify-tool-results.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { errorToObject } from '@codebuff/common/util/object'
1+
import { getErrorObject } from '@codebuff/common/util/error'
22
import { cloneDeep } from 'lodash'
33

44
import { logger } from './logger'
@@ -44,7 +44,7 @@ export function simplifyTerminalCommandResults(
4444
]
4545
} catch (error) {
4646
logger.error(
47-
{ error: errorToObject(error), messageContent },
47+
{ error: getErrorObject(error), messageContent },
4848
'Error simplifying terminal command results',
4949
)
5050
return [

common/src/util/error.ts

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,48 @@
1-
export function getErrorObject(error: any) {
1+
export type ErrorOr<T, E extends ErrorObject = ErrorObject> =
2+
| Success<T>
3+
| Failure<E>
4+
5+
export type Success<T> = {
6+
success: true
7+
value: T
8+
}
9+
10+
export type Failure<E extends ErrorObject = ErrorObject> = {
11+
success: false
12+
error: E
13+
}
14+
15+
export type ErrorObject = {
16+
name: string
17+
message: string
18+
stack?: string
19+
}
20+
21+
export function success<T>(value: T): Success<T> {
222
return {
3-
message: error.message,
4-
stack: error.stack,
5-
name: error.name,
23+
success: true,
24+
value,
625
}
7-
}
26+
}
27+
28+
export function failure(error: any): Failure<ErrorObject> {
29+
return {
30+
success: false,
31+
error: getErrorObject(error),
32+
}
33+
}
34+
35+
export function getErrorObject(error: any): ErrorObject {
36+
if (error instanceof Error) {
37+
return {
38+
name: error.name,
39+
message: error.message,
40+
stack: error.stack,
41+
}
42+
}
43+
44+
return {
45+
name: 'Error',
46+
message: `${error}`,
47+
}
48+
}

common/src/util/object.ts

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { isEqual, mapValues, union } from 'lodash'
22

33
export const removeUndefinedProps = <T extends object>(
4-
obj: T
5-
): { [K in keyof T as T[K] extends undefined ? never : K]: Exclude<T[K], undefined> } => {
4+
obj: T,
5+
): {
6+
[K in keyof T as T[K] extends undefined ? never : K]: Exclude<T[K], undefined>
7+
} => {
68
const newObj: any = {}
79

810
for (const key of Object.keys(obj)) {
@@ -117,17 +119,3 @@ export function assert(condition: boolean, message: string): asserts condition {
117119
throw new Error(`Assertion failed: ${message}`)
118120
}
119121
}
120-
121-
export function errorToObject(value: any) {
122-
if (value instanceof Error) {
123-
// Copy *all* own props, including non-enumerables
124-
return Object.getOwnPropertyNames(value).reduce<Record<string, unknown>>(
125-
(acc, key) => {
126-
acc[key] = (value as any)[key]
127-
return acc
128-
},
129-
{},
130-
)
131-
}
132-
return value
133-
}

evals/git-evals/run-git-evals.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import path from 'path'
44

55
import { disableLiveUserInputCheck } from '@codebuff/backend/live-user-inputs'
66
import { promptAiSdkStructured } from '@codebuff/backend/llm-apis/vercel-ai-sdk/ai-sdk'
7-
import { errorToObject } from '@codebuff/common/util/object'
7+
import { getErrorObject } from '@codebuff/common/util/error'
88
import { withTimeout } from '@codebuff/common/util/promise'
99
import { generateCompactId } from '@codebuff/common/util/string'
1010
import { cloneDeep } from 'lodash'
@@ -250,7 +250,7 @@ Explain your reasoning in detail. Do not ask Codebuff to git commit changes.`,
250250
judging_results: {
251251
analysis: `Judging failed due to error:\n${JSON.stringify(
252252
judgingError instanceof Error
253-
? errorToObject(judgingError)
253+
? getErrorObject(judgingError)
254254
: judgingError,
255255
)}`,
256256
strengths: [],

npm-app/src/utils/changes.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import fs from 'fs'
22
import path from 'path'
33

44
import { isFileIgnored } from '@codebuff/common/project-file-tree'
5-
import { errorToObject } from '@codebuff/common/util/object'
5+
import { getErrorObject } from '@codebuff/common/util/error'
66
import { applyPatch } from 'diff'
77

88
import type { FileChanges } from '@codebuff/common/actions'
@@ -54,7 +54,7 @@ export function applyChanges(projectRoot: string, changes: FileChanges) {
5454
} catch (error) {
5555
console.error(
5656
`Failed to apply patch to ${filePath}:`,
57-
errorToObject(error),
57+
getErrorObject(error),
5858
content,
5959
)
6060
invalid.push(filePath)

0 commit comments

Comments
 (0)