Skip to content

Commit

Permalink
Accumulate erros on decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoDaniels committed May 18, 2021
1 parent df7ccef commit 85c5cf5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "environment-decoder",
"description": "A decoder for the process.env",
"author": "Marco Daniel Martins <[email protected]>",
"version": "1.0.0",
"version": "1.1.0",
"license": "MIT",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
17 changes: 12 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,21 @@ export const environmentDecoder = <S>(schemaType: S): DecodeType<S> => {
throw `Missing environment variables: \n${missing.join(`\n`)}\n`
}

return schema
const decoderErrors = schema
.map(([key, decoder]: [string, any]) => {
try {
const value = environment[key]
return [key, decoder(value)]
decoder(environment[key])
return false
} catch (message) {
throw `Error for environment "${key}": ${message}\n`
return `${key}: ${message}`
}
})
.reduce((acc, [key, value]) => ({...acc, [key]: value}), {})
.filter((decoder) => decoder)
if (decoderErrors.length) {
throw `Decoder errors: \n${decoderErrors.join(`\n`)}\n`
}

return schema
.map(([key, decoder]: [string, any]) => [key, decoder(environment[key])])
.reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {})
}

0 comments on commit 85c5cf5

Please sign in to comment.