Skip to content

Commit 85c5cf5

Browse files
committed
Accumulate erros on decoding
1 parent df7ccef commit 85c5cf5

2 files changed

Lines changed: 13 additions & 6 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "environment-decoder",
33
"description": "A decoder for the process.env",
44
"author": "Marco Daniel Martins <marcodanielmartins@gmail.com>",
5-
"version": "1.0.0",
5+
"version": "1.1.0",
66
"license": "MIT",
77
"main": "dist/index.js",
88
"types": "dist/index.d.ts",

src/index.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,21 @@ export const environmentDecoder = <S>(schemaType: S): DecodeType<S> => {
6060
throw `Missing environment variables: \n${missing.join(`\n`)}\n`
6161
}
6262

63-
return schema
63+
const decoderErrors = schema
6464
.map(([key, decoder]: [string, any]) => {
6565
try {
66-
const value = environment[key]
67-
return [key, decoder(value)]
66+
decoder(environment[key])
67+
return false
6868
} catch (message) {
69-
throw `Error for environment "${key}": ${message}\n`
69+
return `${key}: ${message}`
7070
}
7171
})
72-
.reduce((acc, [key, value]) => ({...acc, [key]: value}), {})
72+
.filter((decoder) => decoder)
73+
if (decoderErrors.length) {
74+
throw `Decoder errors: \n${decoderErrors.join(`\n`)}\n`
75+
}
76+
77+
return schema
78+
.map(([key, decoder]: [string, any]) => [key, decoder(environment[key])])
79+
.reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {})
7380
}

0 commit comments

Comments
 (0)