Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failing to decode enum throws incorrect error #227

Open
rausnitz opened this issue Jun 9, 2022 · 0 comments
Open

Failing to decode enum throws incorrect error #227

rausnitz opened this issue Jun 9, 2022 · 0 comments
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@rausnitz
Copy link
Sponsor Contributor

rausnitz commented Jun 9, 2022

Describe the bug

When decoding a Postgres value into a RawRepresentable enum type, if the raw value does not match a valid case of the enum, the following error description is generated:

"Unexpected data type: TEXT. Expected jsonb/json."

That is not an accurate description of the error.

To Reproduce

Reproduced here and copied below: centaurlabs@3757760#diff-d6c116bb5c868c4835aa93294335dd0ad2125664273437278a43a23ef2cdb17cR149-R170.

func testUnsuccessfullyDecodeModelWithEnum() throws {
    let conn = try PostgresConnection.test(on: self.eventLoop).wait()
    defer { try! conn.close().wait() }

    let rows = try conn.query("SELECT 'bar'::text as foo").wait()
    let row = rows[0]
    
    struct Test: Codable {
        var foo: TestEnum
        
        enum TestEnum: String, Codable {
            case foo
        }
    }
    
    do {
        _ = try row.sql().decode(model: Test.self)
    } catch {
        let errorDescription: String = (error as CustomStringConvertible).description
        XCTAssert(errorDescription == "Unexpected data type: TEXT. Expected jsonb/json.")
    }
}

Expected behavior

The error should say something accurate and not default to saying JSON was expected.

It would be great if it said something like Value "bar" is not a valid raw value for TestEnum. This is tricky because it does not seem straightforward to determine if type on this line is RawRepresentable or anything else that would indicate this is an enum. Maybe someone else will know a good way to do this.

If not, another solution would be to replace "jsonb/json" with String(describing: type) on this line.

Or we could throw a new error altogether.

Environment

Any environment that can run the tests should be able to run the one I added as a demo.

@rausnitz rausnitz added the bug Something isn't working label Jun 9, 2022
@0xTim 0xTim added the good first issue Good for newcomers label Jun 15, 2022
@VaporBot VaporBot added this to To do in Beginner Issues Jun 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
Development

No branches or pull requests

2 participants