UBERF-11111: Pass original error to logger

Signed-off-by: Artem Savchenko <armisav@gmail.com>
This commit is contained in:
Artem Savchenko 2025-05-26 12:04:58 +07:00
parent e4361fa64d
commit 54b70e1a7e
2 changed files with 11 additions and 10 deletions

View File

@ -43,6 +43,7 @@ describe('Retryable decorator', () => {
it('should retry failed operations', async () => {
// Create a test class with decorated method that fails initially then succeeds
const error = new Error('First attempt failed')
class TestService {
callCount = 0
@ -50,7 +51,7 @@ describe('Retryable decorator', () => {
async testMethod (param1: string, param2: number): Promise<string> {
this.callCount++
if (this.callCount === 1) {
throw new Error('First attempt failed')
throw error
}
return `${param1}-${param2}`
}
@ -68,7 +69,7 @@ describe('Retryable decorator', () => {
expect(mockLogger.warn).toHaveBeenCalledWith(
expect.stringContaining('testMethod failed'),
expect.objectContaining({
error: 'First attempt failed',
error,
attempt: 1
})
)

View File

@ -71,25 +71,25 @@ export async function withRetry<T> (
while (attempt <= config.maxRetries) {
try {
return await operation()
} catch (err: any) {
lastError = err
} catch (error: any) {
lastError = error
const isLastAttempt = attempt >= config.maxRetries
if (isLastAttempt) {
logger.error(`${operationName} failed after ${attempt} attempts`, {
error: err.message,
error,
attempt,
maxRetries: config.maxRetries
})
throw err
throw error
}
if (!config.isRetryable(err)) {
if (!config.isRetryable(error)) {
logger.error(`${operationName} failed with non-retriable error`, {
error: err.message,
error,
attempt,
maxRetries: config.maxRetries
})
throw err
throw error
}
// Calculate next delay with jitter
@ -100,7 +100,7 @@ export async function withRetry<T> (
const actualDelay = Math.min(delayMs + jitterAmount, config.maxDelayMs)
logger.warn(`${operationName} failed, retrying in ${Math.round(actualDelay)}ms`, {
error: err.message,
error,
attempt,
nextAttempt: attempt + 1,
delayMs: Math.round(actualDelay)