Skip to content

Commit 75c9f37

Browse files
committed
refactor: improve error handling across modules
- Add custom error types for app_data, bridge, and database modules - Enhance error reporting in reddit_to_telegram main function - Replace generic error strings with structured error variants
1 parent 54f0663 commit 75c9f37

File tree

4 files changed

+38
-14
lines changed

4 files changed

+38
-14
lines changed

src/app_data.gleam

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ pub type AppData {
1212
)
1313
}
1414

15-
pub fn get() -> Result(AppData, String) {
15+
pub type Error {
16+
MissingEnvVar(String)
17+
}
18+
19+
pub fn get() -> Result(AppData, Error) {
1620
dotenv_gleam.config()
1721

1822
use username <- result.try(get_env("REDDIT_USERNAME"))
@@ -30,8 +34,8 @@ pub fn get() -> Result(AppData, String) {
3034
)
3135
}
3236

33-
fn get_env(key: String) -> Result(String, String) {
37+
fn get_env(key: String) -> Result(String, Error) {
3438
key
3539
|> envoy.get
36-
|> result.map_error(fn(_) { "Missing " <> key <> " environment variable" })
40+
|> result.map_error(fn(_) { MissingEnvVar(key) })
3741
}

src/bridge.gleam

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import gleam/dynamic/decode
22
import gleam/json
33
import gleam/result
4-
import gleam/string
54
import reddit
65
import simplifile
76

@@ -16,17 +15,22 @@ pub type Bridge {
1615
)
1716
}
1817

19-
pub fn get() -> Result(List(Bridge), String) {
18+
pub type Error {
19+
ReadFileError(simplifile.FileError)
20+
DecodeError(json.DecodeError)
21+
}
22+
23+
pub fn get() -> Result(List(Bridge), Error) {
2024
simplifile.read("./bridges.json")
21-
|> result.map_error(fn(_) { "File bridges.json not found" })
25+
|> result.map_error(fn(error) { ReadFileError(error) })
2226
|> result.try(bridges_decoder)
2327
}
2428

25-
fn bridges_decoder(json_bridges: String) -> Result(List(Bridge), String) {
29+
fn bridges_decoder(json_bridges: String) -> Result(List(Bridge), Error) {
2630
bridge_decoder()
2731
|> decode.list
2832
|> json.parse(json_bridges, _)
29-
|> result.map_error(fn(e) { "Couldn't decode bridges: " <> string.inspect(e) })
33+
|> result.map_error(fn(e) { DecodeError(e) })
3034
}
3135

3236
fn bridge_decoder() -> decode.Decoder(Bridge) {

src/database.gleam

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@ import gleam/result
55
import gleam/string
66
import sqlight.{type Connection}
77

8-
pub fn connect() {
8+
pub type Error {
9+
OpenError(sqlight.Error)
10+
}
11+
12+
pub fn connect() -> Result(Connection, Error) {
913
use connection <- result.map(
1014
sqlight.open("file:./db/data.sqlite3")
11-
|> result.map_error(fn(error) { error.message }),
15+
|> result.map_error(fn(error) { OpenError(error) }),
1216
)
1317

1418
let _ = setup(connection)

src/reddit_to_telegram.gleam

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,27 @@ import gleam/list
77
import gleam/option.{type Option, None, Some}
88
import gleam/pair
99
import gleam/result
10+
import gleam/string
1011
import reddit
1112
import sqlight
1213
import telegram
1314

15+
type Error {
16+
AppDataError(app_data.Error)
17+
BridgeError(bridge.Error)
18+
DatabaseError(database.Error)
19+
}
20+
1421
pub fn main() {
1522
let result = {
1623
io.println("Loading app data...")
17-
use app_data <- result.try(app_data.get())
24+
use app_data <- result.try(app_data.get() |> result.map_error(AppDataError))
1825
io.println("Loading bridges...")
19-
use bridges <- result.try(bridge.get())
26+
use bridges <- result.try(bridge.get() |> result.map_error(BridgeError))
2027
io.println("Connecting to database...")
21-
use database <- result.map(database.connect())
28+
use database <- result.map(
29+
database.connect() |> result.map_error(DatabaseError),
30+
)
2231

2332
start(app_data, bridges, database)
2433
}
@@ -38,7 +47,10 @@ pub fn main() {
3847
}
3948
}
4049
}
41-
Error(error) -> io.println("Error: " <> error)
50+
Error(error) ->
51+
error
52+
|> string.inspect
53+
|> io.println
4254
}
4355
}
4456

0 commit comments

Comments
 (0)