Skip to content

Commit

Permalink
🚸 Improve error message for wrong database/user
Browse files Browse the repository at this point in the history
  • Loading branch information
giacomocavalieri committed Aug 11, 2024
1 parent cb57676 commit 5ead037
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 30 deletions.
28 changes: 24 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,33 @@

## Unreleased

- Use `postgres` as the default user in case the `PGUSER` env variable is not
set.
- Add support for `varchar` and `bpchar` types.
- Squirrel now uses `"postgres"` as the default user in case the `PGUSER`
environment variable is not set.
([Giacomo Cavalieri](https://github.com/giacomocavalieri))

- Added support for the `varchar` and `bpchar` types, mapped to Gleam `String`s.
([Giacomo Cavalieri](https://github.com/giacomocavalieri))

- Improved the error message in case Squirrel cannot connect to the Postgres
server with the given database and username.
Instead of a not-really-helpful `Cannot receive message: Closed` the error now
properly explains what went wrong and hot to solve the issue:

```
Error: Cannot connect
I couldn't connect to database `database` with user `postgres`.
Hint: You can change the default user and database by setting the `PGUSER` and
`PGDATABASE` environment variables.
```

([Giacomo Cavalieri](https://github.com/giacomocavalieri))

## v1.0.1 - 2024-08-09

- Fix a bug with the code generation of `Option` types.
- Fixed a bug with the code generation of `Option` types.
([Giacomo Cavalieri](https://github.com/giacomocavalieri))

## v1.0.0 - 2024-08-09

Expand Down
18 changes: 14 additions & 4 deletions src/squirrel/internal/database/postgres.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ import gleam/string
import squirrel/internal/database/postgres_protocol as pg
import squirrel/internal/error.{
type Error, type Pointer, type ValueIdentifierError, ByteIndex,
CannotParseQuery, PgCannotAuthenticate, PgCannotDecodeReceivedMessage,
PgCannotDescribeQuery, PgCannotReceiveMessage, PgCannotSendMessage, Pointer,
QueryHasInvalidColumn, QueryHasUnsupportedType,
CannotParseQuery, PgCannotAuthenticate, PgCannotConnectUserDatabase,
PgCannotDecodeReceivedMessage, PgCannotDescribeQuery, PgCannotReceiveMessage,
PgCannotSendMessage, Pointer, QueryHasInvalidColumn, QueryHasUnsupportedType,
}
import squirrel/internal/eval_extra
import squirrel/internal/gleam
Expand Down Expand Up @@ -254,7 +254,17 @@ fn authenticate(connection: ConnectionOptions) -> Db(Nil) {
_ -> unexpected_message(PgCannotAuthenticate, "AuthenticationOk", msg)
})

use _ <- eval.try(wait_until_ready())
use _ <- eval.try(
wait_until_ready()
// In case there's a receive error while waiting for the server to be ready
// we want to display a more helpful error message because the problem here
// must be with an invalid username/database combination.
|> eval.replace_error(PgCannotConnectUserDatabase(
user: connection.user,
database: connection.database,
)),
)

eval.return(Nil)
}

Expand Down
64 changes: 42 additions & 22 deletions src/squirrel/internal/error.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ import simplifile

pub type Error {
// --- POSTGRES RELATED ERRORS -----------------------------------------------
/// When the server immediately closes the connection right after we try to
/// connect using the given username and database.
///
PgCannotConnectUserDatabase(user: String, database: String)

/// When authentication workflow goes wrong.
/// TODO)) For now I only support no authentication so people might report
/// this issue.
///
PgCannotAuthenticate(expected: String, got: String)

Expand Down Expand Up @@ -127,6 +130,31 @@ pub fn to_doc(error: Error) -> Document {
// that is actually printed and we do not have to make any effort to add and
// print new errors.
let printable_error = case error {
PgCannotConnectUserDatabase(user: user, database: database) ->
printable_error("Cannot connect")
|> add_paragraph(
"I couldn't connect to database "
<> style_inline_code(database)
<> " with user "
<> style_inline_code(user)
<> ".",
)
|> hint(
"You can change the default user and database by setting the "
<> style_inline_code("PGUSER")
<> " and "
<> style_inline_code("PGDATABASE")
<> " environment variables.",
)

PgCannotAuthenticate(expected: expected, got: got) ->
printable_error("Cannot authenticate")
|> add_paragraph(
"I ran into an unexpected problem while trying to authenticate with the
Postgres server. This is most definitely a bug!",
)
|> report_bug("Expected: " <> expected <> ", Got: " <> got)

PgCannotSendMessage(reason: reason) ->
printable_error("Cannot send message")
|> add_paragraph(
Expand All @@ -151,6 +179,18 @@ database server.",
)
|> report_bug(reason)

PgCannotDescribeQuery(
file: file,
query_name: query_name,
expected: expected,
got: got,
) ->
printable_error("Cannot inspect query")
|> add_paragraph("I ran into an unexpected problem while trying to figure
out the types of query " <> style_inline_code(query_name) <> "
defined in " <> style_file(file) <> ". This is most definitely a bug!")
|> report_bug("Expected: " <> expected <> ", Got: " <> got)

CannotReadFile(file: file, reason: reason) ->
printable_error("Cannot read file")
|> add_paragraph(
Expand Down Expand Up @@ -274,26 +314,6 @@ contain lowercase letters, numbers and underscores.",
starting_line: starting_line,
)
|> maybe_hint(hint)

PgCannotAuthenticate(expected: expected, got: got) ->
printable_error("Cannot authenticate")
|> add_paragraph(
"I ran into an unexpected problem while trying to authenticate with the
Postgres server. This is most definitely a bug!",
)
|> report_bug("Expected: " <> expected <> ", Got: " <> got)

PgCannotDescribeQuery(
file: file,
query_name: query_name,
expected: expected,
got: got,
) ->
printable_error("Cannot inspect query")
|> add_paragraph("I ran into an unexpected problem while trying to figure
out the types of query " <> style_inline_code(query_name) <> "
defined in " <> style_file(file) <> ". This is most definitely a bug!")
|> report_bug("Expected: " <> expected <> ", Got: " <> got)
}

printable_error_to_doc(printable_error)
Expand Down

0 comments on commit 5ead037

Please sign in to comment.