JasonShin / sqlx-ts

node.js compile-time SQL validations & type generations
https://jasonshin.github.io/sqlx-ts/
MIT License
185 stars 6 forks source link

Improve error handling #132

Open simplenotezy opened 4 months ago

simplenotezy commented 4 months ago

I think the library needs better error handling. I've often been stuck for quite a while with simple queries that fail with an unknown rust error.

Now I am facing this one, and I am not sure why:

thread 'main' panicked at src/ts_generator/sql_parser/expressions/translate_expr.rs:189:59:
called `Option::unwrap()` on a `None` value
stack backtrace:
   0:        0x103521b64 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h1f3776e0b5c7517d
   1:        0x103540ef0 - core::fmt::write::heedef092c8c0962e
   2:        0x10351e850 - std::io::Write::write_fmt::h7178e8e2ea928914
   3:        0x1035219bc - std::sys_common::backtrace::print::h417292deb95532ed
   4:        0x103522b18 - std::panicking::default_hook::{{closure}}::h0cb68f1228c4613a
   5:        0x10352280c - std::panicking::default_hook::h24535936bc1f51de
   6:        0x1035233d0 - std::panicking::rust_panic_with_hook::h5db4d2345b297bed
   7:        0x103522dd4 - std::panicking::begin_panic_handler::{{closure}}::h3fd558f09a0d5492
   8:        0x103521fec - std::sys_common::backtrace::__rust_end_short_backtrace::hfc76eebe1ce501b2
   9:        0x103522b70 - _rust_begin_unwind
  10:        0x1035641d0 - core::panicking::panic_fmt::hc2b459a5bd3dce66
  11:        0x103564250 - core::panicking::panic::h65d5df1c072e7907
  12:        0x103564188 - core::option::unwrap_failed::hc6c070017bc3706d
  13:        0x10307a2a0 - sqlx_ts::ts_generator::sql_parser::expressions::translate_expr::translate_expr::{{closure}}::h8167647399229f64
  14:        0x103165b3c - sqlx_ts::ts_generator::sql_parser::translate_query::translate_query::{{closure}}::hbb13567b6f258221
  15:        0x1031672bc - sqlx_ts::ts_generator::sql_parser::translate_stmt::translate_stmt::{{closure}}::hb018146488cc502c
  16:        0x103161e3c - sqlx_ts::ts_generator::generator::generate_ts_interface::{{closure}}::h0437b2601b8838b8
  17:        0x10316c554 - sqlx_ts::main::{{closure}}::hdfdc8c390af37a65
  18:        0x103159694 - tokio::runtime::park::CachedParkThread::block_on::hfff00a85b0473a50
  19:        0x103134028 - tokio::runtime::context::runtime::enter_runtime::h7ca324ddb8c44e99
  20:        0x1030c5b0c - tokio::runtime::runtime::Runtime::block_on::h04bf0b47b449c7d4
  21:        0x1030fb9ec - sqlx_ts::main::he63bae760152502e
  22:        0x1031456c4 - std::sys_common::backtrace::__rust_begin_short_backtrace::heca7c122cd8ebc36
  23:        0x1030af4ac - std::rt::lang_start::{{closure}}::h889d2a596bb18acf
  24:        0x103519484 - std::rt::lang_start_internal::hecc68fef83c8f44d
  25:        0x1030fbaf4 - _main

With this code:

  const usersQuery = await dataSource.query({
    text: sql`
  SELECT
    u.name
  FROM
    "user" u
  WHERE
    u.id = ANY($1)
`,
    values: [ids],
  });

p.s. Impressive how fast you progress with this package, good job!

simplenotezy commented 4 months ago

For some reason this fixes the error:

  const usersQuery = await dataSource.query({
    text: sql`
  SELECT
+    name::string AS "name"
-    name
  FROM
    "user"
  WHERE
    id = ANY($1)
`,
    values: [ids],
  });
JasonShin commented 3 months ago

Separate to this particular problem, I've improved error message handlings in several areas including DB connection issues https://github.com/JasonShin/sqlx-ts/pull/156/files