77import Foundation
88import NIO
99
10+ public enum DotEnvError : Error {
11+ case fileCouldNotBeRead( String , String . Encoding )
12+ }
13+
1014/// Either provides an EventLoopGroup or indicate to create a new one
1115public enum EventLoopGroupSource {
1216 /// Provided EventLoopGroup
1317 case provided( EventLoopGroup )
1418 /// Create a EventLoopGroup
1519 case createNew
1620}
17-
1821/// Represents a `KEY=VALUE` pair in a dotenv file.
1922public struct Line : CustomStringConvertible , Equatable {
2023 /// The key.
@@ -28,7 +31,6 @@ public struct Line: CustomStringConvertible, Equatable {
2831 return " \( self . key) = \( self . value) "
2932 }
3033}
31-
3234/// An environment variable loader.
3335///
3436/// You can either read the file and then load it or load in one step.
@@ -236,11 +238,11 @@ public struct DotEnv {
236238 public static func load( path: String ,
237239 suffix: String ,
238240 encoding: String . Encoding = . utf8,
239- overwrite: Bool = true ) {
240- load ( path: " \( path) . \( suffix) " ,
241+ overwrite: Bool = true ) throws {
242+ try load ( path: " \( path) . \( suffix) " ,
241243 encoding: encoding,
242244 overwrite: overwrite)
243- load ( path: path,
245+ try load ( path: path,
244246 encoding: encoding,
245247 overwrite: overwrite)
246248 }
@@ -258,11 +260,15 @@ public struct DotEnv {
258260 /// - overwrite: Set to false to prevent overwiting current environment variables
259261 public static func load( path: String ,
260262 encoding: String . Encoding = . utf8,
261- overwrite: Bool = true ) {
262- let file = try ! String ( contentsOfFile: path, encoding: encoding)
263- var parser = StringParser ( source: file)
264- let dotenv = Self . init ( lines: parser. parse ( ) )
265- dotenv. load ( overwrite: overwrite)
263+ overwrite: Bool = true ) throws {
264+ do {
265+ let file = try String ( contentsOfFile: path, encoding: encoding)
266+ var parser = StringParser ( source: file)
267+ let dotenv = Self . init ( lines: parser. parse ( ) )
268+ dotenv. load ( overwrite: overwrite)
269+ } catch {
270+ throw DotEnvError . fileCouldNotBeRead ( path, encoding)
271+ }
266272 }
267273
268274 /// Reads a `DotEnv` file from the supplied path.
@@ -282,10 +288,14 @@ public struct DotEnv {
282288 /// - path: Absolute or relative path of the dotenv file.
283289 /// - encoding: Encoding of the file
284290 /// - returns: `DotEnv`
285- public static func read( path: String , encoding: String . Encoding = . utf8) -> DotEnv {
286- let file = try ! String ( contentsOfFile: path, encoding: encoding)
287- var parser = StringParser ( source: file)
288- return . init( lines: parser. parse ( ) )
291+ public static func read( path: String , encoding: String . Encoding = . utf8) throws -> DotEnv {
292+ do {
293+ let file = try String ( contentsOfFile: path, encoding: encoding)
294+ var parser = StringParser ( source: file)
295+ return . init( lines: parser. parse ( ) )
296+ } catch {
297+ throw DotEnvError . fileCouldNotBeRead ( path, encoding)
298+ }
289299 }
290300
291301 /// All `KEY=VALUE` pairs found in the file.
0 commit comments