brianc / node-pg-native

Native (C/C++) bindings to PostgreSQL with sync and async options.
247 stars 44 forks source link

Errors appear different to node-postgres #24

Closed FlamingTempura closed 9 years ago

FlamingTempura commented 9 years ago

Errors returned by client.query(function (err) {}) in node-pg-native look very different to errors in node-postgres, which means it cannot be used as a drop in replacement where error checking is required (e.g. checking which constraint caused an error).

Example error from node-postgres (caused by a unique index):

{  
    "cause":{  
        "name":"error",
        "length":273,
        "severity":"ERROR",
        "code":"23505",
        "detail":"Key (user_id, collection, name)=(578, test-collection-50811574724503, test-model-69687819783576) already exists.",
        "schema":"public",
        "table":"model",
        "constraint":"model_unique_index",
        "file":"nbtinsert.c",
        "line":"406",
        "routine":"_bt_check_unique"
    },
    "length":273,
    "severity":"ERROR",
    "code":"23505",
    "detail":"Key (user_id, collection, name)=(578, test-collection-50811574724503, test-model-69687819783576) already exists.",
    "schema":"public",
    "table":"model",
    "constraint":"model_unique_index",
    "file":"nbtinsert.c",
    "line":"406",
    "routine":"_bt_check_unique"
}

The same error from node-pg-native looks very different:

{  
    "cause":{  
        "severity":"ERROR",
        "sqlState":"23505",
        "messagePrimary":"duplicate key value violates unique constraint \"model_unique_index\"",
        "messageDetail":"Key (user_id, collection, name)=(582, test-collection-21656118379906, test-model-22533511230722) already exists.",
        "sourceFile":"nbtinsert.c",
        "sourceLine":"406",
        "sourceFunction":"_bt_check_unique"
    },
    "severity":"ERROR",
    "sqlState":"23505",
    "messagePrimary":"duplicate key value violates unique constraint \"model_unique_index\"",
    "messageDetail":"Key (user_id, collection, name)=(582, test-collection-21656118379906, test-model-22533511230722) already exists.",
    "sourceFile":"nbtinsert.c",
    "sourceLine":"406",
    "sourceFunction":"_bt_check_unique"
}
brianc commented 9 years ago

This is probably something that should be normalized within node-postgres itself rather than here in the native lib. If you wanna take a crack at it over there I'll happily review the PR!

FlamingTempura commented 9 years ago

Thanks for your reply.

I've decided not to pursue this as I've begun using pg-query-stream which isn't compatible with pg-native. I'll close this for now.

Great work on these libraries by the way!