Skip to content

Commit 3d837ee

Browse files
committed
clarify and unify error handling
1 parent 0cd7966 commit 3d837ee

File tree

4 files changed

+12
-13
lines changed

4 files changed

+12
-13
lines changed

multiple-servers/api/api.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"log"
77
"net/http"
8-
"os"
98

109
"github.com/jackc/pgx/v4"
1110
)
@@ -15,10 +14,10 @@ type Config struct {
1514
Port int
1615
}
1716

18-
func Run(config Config) {
17+
func Run(config Config) error {
1918
conn, err := pgx.Connect(context.Background(), config.DatabaseURL)
2019
if err != nil {
21-
log.Fatalf("unable to connect to database: %v\n", err)
20+
return fmt.Errorf("unable to connect to database: %v", err)
2221
}
2322
// Defer closing the connection to when main function exits
2423
defer conn.Close(context.Background())
@@ -35,7 +34,7 @@ func Run(config Config) {
3534
// Add new image to the database
3635
image, err := AddImage(conn, r)
3736
if err != nil {
38-
fmt.Fprintln(os.Stderr, err.Error())
37+
log.Println(err.Error())
3938
// We don't expose our internal errors (i.e. the contents of err) directly to the user for a few reasons:
4039
// 1. It may leak private information (e.g. a database connection string, which may even include a password!), which may be a security risk.
4140
// 2. It probably isn't useful to them to know.
@@ -49,7 +48,7 @@ func Run(config Config) {
4948
// Fetch images from the database
5049
images, err := FetchImages(conn)
5150
if err != nil {
52-
fmt.Fprintln(os.Stderr, err.Error())
51+
log.Println(err.Error())
5352
http.Error(w, "Something went wrong", http.StatusInternalServerError)
5453
return
5554
}
@@ -58,7 +57,7 @@ func Run(config Config) {
5857
}
5958

6059
if responseErr != nil {
61-
fmt.Fprintln(os.Stderr, err.Error())
60+
log.Println(responseErr.Error())
6261
http.Error(w, "Something went wrong", http.StatusInternalServerError)
6362
return
6463
}
@@ -69,5 +68,5 @@ func Run(config Config) {
6968
w.Write(response)
7069
})
7170

72-
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", config.Port), nil))
71+
return http.ListenAndServe(fmt.Sprintf(":%d", config.Port), nil)
7372
}

multiple-servers/cmd/api-server/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ func main() {
1212
log.Fatalln("DATABASE_URL not set")
1313
}
1414

15-
api.Run(api.Config{
15+
log.Fatal(api.Run(api.Config{
1616
DatabaseURL: os.Getenv("DATABASE_URL"),
1717
Port: 8081,
18-
})
18+
}))
1919
}

multiple-servers/cmd/static-server/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ func main() {
1616
log.Fatalln(err)
1717
}
1818

19-
static.Run(static.Config{
19+
log.Fatal(static.Run(static.Config{
2020
Dir: absPath,
2121
Port: 8082,
22-
})
22+
}))
2323
}

multiple-servers/static/static.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type Config struct {
1212
Port int
1313
}
1414

15-
func Run(config Config) {
15+
func Run(config Config) error {
1616
// The "/" path handles everything, so we need to inspect the path (`req.URL.Path`) to be able to
1717
// identify which file to serve.
1818
// https://pkg.go.dev/net/http#ServeMux.Handle
@@ -23,5 +23,5 @@ func Run(config Config) {
2323
http.ServeFile(w, r, path)
2424
})
2525

26-
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", config.Port), nil))
26+
return http.ListenAndServe(fmt.Sprintf(":%d", config.Port), nil)
2727
}

0 commit comments

Comments
 (0)