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

Example: Provide a postgresql example with error handling #2938

Open
vanvuongngo opened this issue Nov 28, 2024 · 0 comments
Open

Example: Provide a postgresql example with error handling #2938

vanvuongngo opened this issue Nov 28, 2024 · 0 comments

Comments

@vanvuongngo
Copy link
Contributor

Hi,

I got stuck to figure out how to get detailed postgresql error message.

function to store data into postgres:

fn store_new_thing(thing: String) -> Result<()> {
    let db_connection = variables::get("db_connection")?;
    let connection = Connection::open(&db_connection)?;

    let params = vec![ParameterValue::Str(thing.clone())];
    connection.execute(SQL_CREATE_NEW_THING, &params)?;

    Ok(())
}

usage

match store_new_thing(serialized) {
    Ok(_) => println!("store new thing succeed"),
    Err(error) => panic!("Failed to save new thing: {:?}", error),
}

I got this output but I want to check the reason. So how can I access for instance the severity or message or routine...
println!("{}", error.causes()) returns None and the function is deprecated.

{
  kind: Db,
  cause: Some(DbError {
    severity: \"ERROR\",
    parsed_severity: Some(Error),
    code: SqlState(E23505),
    message: \"duplicate key value violates unique constraint \\\"new_things_pkey\\\"\",
    detail: Some(\"Key (id)=(...) already exists.\"), 
    hint: None,
    position: None,
    where_: Some(\"SQL function \\\"fn_insert_new\\\" statement 1\"),
    schema: Some(\"public\"),
    table: Some(\"new_things\"),
    column: None, datatype: None, constraint: Some(\"tb_new_things_pkey\"),
    file: Some(\"nbtinsert.c\"),
    line: Some(673),
    routine: Some(\"_bt_check_unique\")
  })
}
  • Spin version 3.0.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant