TanmoySG / wunderDB

A micro JSON-based Data Store inspired by MongoDB.
http://wdb.tanmoysg.com/api/
Apache License 2.0
12 stars 0 forks source link

[Development] Improvements in Error tracing #110

Open TanmoySG opened 1 year ago

TanmoySG commented 1 year ago

wunderDB Error wdbErrors is currently constant, and error messages do not have the ability to show which field/entity/etc caused the error. For example, for Database Missing error (parent db for collection does not exist), the error is

    DatabaseDoesNotExistsError = WdbError{
        ErrCode:        "databaseMissing",
        ErrMessage:     "database with ID doesn't exist",
        HttpStatusCode: 404,
    }

Here ErrMessage is constant/fixed and does not include the id which might not exists. This can be modified to make the message as a format and adding the required ID at the caller level so that the message comes as database with ID [databaseId] doesn't exist.

Possible Fixes/Solutions

The error message can be made into a string format (wherever required, and just plain string wherever not required), eg:

    DatabaseDoesNotExistsError = WdbError{
        ErrCode:        "databaseMissing",
        ErrMessage:     "database with ID [%s] does not exist",
        HttpStatusCode: 404,
    }

Adding a Format(args) method on the wdbError struct to fill in the placeholders (%s / %i / %v) with the required value. Example

func (wdbe WdbError) Format(args ...interface{}) WdbError {
    wdbe.ErrMessage = fmt.Sprintf(wdbe.ErrMessage, args...)
    return wdbe
}

And at caller end

    if exists, _ := wdb.Databases.CheckIfExists(databaseId); !exists {
        return &er.DatabaseDoesNotExistsError.Format(databaseId)
    }

The function name Format can be different to much more specific like Fill or TBD