Skip to content

Commit

Permalink
Refactor importActorProfile function to streamline error handling and…
Browse files Browse the repository at this point in the history
… remove redundant console logs
  • Loading branch information
0marSalah committed Feb 17, 2025
1 parent 739f3ff commit e5bb242
Showing 1 changed file with 1 addition and 17 deletions.
18 changes: 1 addition & 17 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,7 @@ export async function importActorProfile(
console?: Pick<Console, 'log' | 'warn' | 'error'>
onError?: (error: Error, context: { fileName?: string }) => void
} = {}
tarStream: Readable,
options: {
console?: Pick<Console, 'log' | 'warn' | 'error'>
onError?: (error: Error, context: { fileName?: string }) => void
} = {}
): Promise<Record<string, any>> {
const { console = undefined } = options
const { console = undefined } = options
const extract = tar.extract()
const result: Record<string, any> = {}
Expand Down Expand Up @@ -230,22 +224,15 @@ export async function importActorProfile(
if (fileName.endsWith('.json')) {
result[fileName] = JSON.parse(content)
console?.log('Parsed JSON file successfully:', fileName)
console?.log('Parsed JSON file successfully:', fileName)
} else if (fileName.endsWith('.yaml') || fileName.endsWith('.yml')) {
result[fileName] = YAML.parse(content)
} else if (fileName.endsWith('.csv')) {
result[fileName] = content
} else {
console?.warn(`Unsupported file type: ${fileName}, skipping...`)
console?.warn(`Unsupported file type: ${fileName}, skipping...`)
}
} catch (error: any) {
const errorMessage = `Error processing file ${fileName}: ${error.message}`
if (onError) {
onError(new Error(errorMessage), { fileName })
} else {
reject(new Error(errorMessage))
}
next(error)
} finally {
next() // Always continue
}
Expand All @@ -260,19 +247,16 @@ export async function importActorProfile(
})

extract.on('finish', () => {
console?.log('All files processed successfully.')
console?.log('All files processed successfully.')
resolve(result)
})

extract.on('error', (error) => {
console?.error('Error during tar extraction:', error.message)
console?.error('Error during tar extraction:', error.message)
reject(new Error('Failed to extract tar file.'))
})

tarStream.on('error', (error) => {
console?.error('Error in tar stream:', error.message)
console?.error('Error in tar stream:', error.message)
reject(new Error('Failed to process tar stream.'))
})
Expand Down

0 comments on commit e5bb242

Please sign in to comment.