File tree Expand file tree Collapse file tree 4 files changed +38
-14
lines changed
Expand file tree Collapse file tree 4 files changed +38
-14
lines changed Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff line change 11import gleam/dynamic/decode
22import gleam/json
33import gleam/result
4- import gleam/string
54import reddit
65import 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
3236fn bridge_decoder ( ) -> decode . Decoder ( Bridge ) {
Original file line number Diff line number Diff line change @@ -5,10 +5,14 @@ import gleam/result
55import gleam/string
66import 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 )
Original file line number Diff line number Diff line change @@ -7,18 +7,27 @@ import gleam/list
77import gleam/option . { type Option , None , Some }
88import gleam/pair
99import gleam/result
10+ import gleam/string
1011import reddit
1112import sqlight
1213import telegram
1314
15+ type Error {
16+ AppDataError ( app_data . Error )
17+ BridgeError ( bridge . Error )
18+ DatabaseError ( database . Error )
19+ }
20+
1421pub 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
You can’t perform that action at this time.
0 commit comments