alekseyl / niceql

niceql gem repo. Nice, colorized SQL formatting in ruby.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Undefined method [] for NilClass in prettify_pg_err

jhirn opened this issue · comments

Hello again @alekseyl

I'm getting a NilClass error with 0.2.0 when parsing out the error logs. It's basically swallowing the real log output so I've had to roll back to 1.0.25 for now.

I was able to trace it to this here where it's getting the line at index 3 but it's only a 3 item array.It does correctly detect that I'm using postgres.

Hopefully an easy solution for you but I'm not sure what format is being expected here.

From: /Users/jhirn/.asdf/installs/ruby/2.7.2/lib/ruby/gems/2.7.0/gems/niceql-0.2.0/lib/niceql.rb:84 Niceql::Prettifier.prettify_pg_err:

    79:       return err if err[/LINE \d+/].nil?
    80:       err_line_num = err[/LINE \d+/][5..-1].to_i
    81: 
    82:       #
    83:       binding.pry
 => 84:       start_sql_line = err.lines[3][/(HINT|DETAIL)/] ? 4 : 3
    85:       err_body = start_sql_line < err.lines.length ? err.lines[start_sql_line..-1] : original_sql_query&.lines
    86: 
    87: 
    88:       # this means original query is missing so it's nothing to prettify
    89:       return err unless err_body

[1] pry(Niceql::Prettifier)> err
=> "PG::UndefinedTable: ERROR:  missing FROM-clause entry for table \"product_type\"\nLINE 1: ...t_id\" = $13 AND \"products\".\"carrier_id\" = $14 AND \"product_t...\n                                                             ^\n"
[2] pry(Niceql::Prettifier)> ap err.lines
[
    [0] "PG::UndefinedTable: ERROR:  missing FROM-clause entry for table \"product_type\"\n",
    [1] "LINE 1: ...t_id\" = $13 AND \"products\".\"carrier_id\" = $14 AND \"product_t...\n",
    [2] "                                                             ^\n"
]
=> nil

Hm, and 1.0.25 workerd fine for this same error message, can you get it raw?
And also can you give me you Gemfile spec, plz.

Yes 1.0.25 (err, 0.1.25 my mistype above) works just fine for me minus the deprecation warning about connection_config

It's only 0.2.0 that's giving me an error. The Gemfile is a little big for the project but we're using Rails 6.1.4.1 and friends so the appropriate ActiveRecord versions and friends. We're also using active_record_extended 2.0.3 but non of it's features for this query.

It's actually happening in a number of places and was hard to track down but the raw error is in there as well :

[
    [0] "PG::UndefinedTable: ERROR:  missing FROM-clause entry for table \"product_type\"\n",
    [1] "LINE 1: ...t_id\" = $13 AND \"products\".\"carrier_id\" = $14 AND \"product_t...\n",
    [2] "                                                             ^\n"
]

There's just not an entry for line[3]. It looks like is searching for HINT/DETAIL and I definitely don't have that but I'm not sure if its because it was configured that way. Just ramping up on this code base. I can try to produce a smaller example but might not get to it today.

Thanks so much for being responsive. Let me know what else I can provide besides a more isolated example.

@jhirn fixed in version 0.3.0

Btw, I think the prettifier wasn't even applied in your case, when this error occured.
Plz try this version and give me a feedback ))

Works well and now my SQL is as dapper as your avatar! 😉

Thanks @alekseyl!!