Skip to content

Feature: Use Destructured Error Handling #485

@jdgamble555

Description

@jdgamble555

Is your feature request related to a problem?

Nobody wants to use try / catch every single time they make a call to Surreal DB.

It should return data and error to prevent that. There is a reason supabase uses it, and even Nuxt itself uses it.

const { data, status, error, refresh, clear } = await useFetch('/api/modules', {
  pick: ['title'],
})
const { data } = await supabase.from("instruments").select();
return {
  instruments: data ?? [],
};

What looks easier to handle for DX?

1.

try {
    await db.connect(config.surrealUrl, {
        namespace: config.surrealNamespace,
        database: config.surrealDatabase
    })
} catch (error) {
    if (error instanceof Error) {
        console.error(error)
        return {
            error,
            data: null
        }
    }
    return {
        error: new Error('Unknown connection error'),
        data: null
    }
}

2.

const { data, error } = await db.connect(config.surrealUrl, {
        namespace: config.surrealNamespace,
        database: config.surrealDatabase
    })

Describe the solution

Have the SDK return error objects instead of using throw internally. While there will be refactoring, 99% of developers will apreciate this approach and be willing to do it IMHO.

const { data, error } = await db.connect(config.surrealUrl, {
        namespace: config.surrealNamespace,
        database: config.surrealDatabase
    })

It is Type Safe by default, and way easier to handle promise rejections etc.

J

Alternative methods

Even Theo from YT has his own try / catch replacement.

https://gist.github.com/t3dotgg/a486c4ae66d32bf17c09c73609dacc5b

Try / Catch is terrible DX!

SurrealDB version

2.3.1

JavaScript SDK version

1.3.2

Contact Details

[email protected]

Is there an existing issue for this?

  • I have searched the existing issues

Code of Conduct (repository /surrealdb.js)

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions