Skip to content

Best-effort mode even if there is an error #59

@mrflip

Description

@mrflip

I am using the prettier plugin programmatically to reformat the queries we send for logging/debugging purposes, and so probably the most important queries we'd like to prettify are the ones that won't perfectly parse.

Is there any straightforward way to have a 'best-effort' mode that will do what it can even if the parser can't proceed past a certain point?

Compare debugging this:

error: DB problem 'syntax error at or near "on"' sending query:    
    insert into "ndd" ("crTC", "handle", "id", "idkey", "image0ID", "locID", "note0", "okey", "productID", "purchaseID", "purchasingID", "quantity", "srcID", "steptag", "title", "tkvTC", "typename", "upTC", "vendorID", "viz") select 'val_1'::varchar AS "crTC", 'val_2'::varchar AS "handle", 'val_3'::varchar AS "id", 'val_4'::varchar AS "idkey", 'val_5'::varchar AS "image0ID", 'val_6'::varchar AS "locID", 'val_7'::varchar AS "note0", 'val_8'::varchar AS "okey", 'val_9'::varchar AS "productID", 'val_10'::varchar AS "purchaseID", 'val_11'::varchar AS "purchasingID", 12::integer AS "quantity", 'val_13'::varchar AS "srcID", 'val_14' AS "steptag", 'val_15'::varchar AS "title", 'val_16'::varchar AS "tkvTC", 'val_17'::varchar AS "typename", 'val_18'::varchar AS "upTC", 'val_19'::varchar AS "vendorID", 'val_20' AS "viz" on conflict do nothing on conflict ("true") do update set "image0ID" = 'val_21',"note0" = 'val_22',"quantity" = 'val_23',"steptag" = 'val_24',"title" = 'val_25',"viz" = 'val_26' returning "crTC", "handle", "id", "idkey", "image0ID", "locID", "note0", "okey", "productID", "purchaseID", "purchasingID", "quantity", "srcID", "steptag", "title", "tkvTC", "typename", "upTC", "vendorID", "viz" "handle", "id", "idkey", "image0ID", "locID", "note0", "okey", "productID", "purchaseID", "purchasingID", "quantity", "srcID", "steptag", "title", "tkvTC", "typename", "upTC", "vendorID", "viz") select var_1::varchar AS "crTC", var_2::varchar AS "handle", var_3::varchar AS "id", var_4::varchar AS "idkey", var_5::varchar AS "image0ID", var_6::varchar AS "locID", var_7::varchar AS "note0", var_8::varchar AS "okey", var_9::varchar AS "productID", var_10::varchar AS "purchaseID", var_11::varchar AS "purchasingID", var_12::integer AS "quantity", var_13::varchar AS "srcID", var_14 AS "steptag", var_15::varchar AS "title", var_16::varchar AS "tkvTC", var_17::varchar AS "typename", var_18::varchar AS "upTC", var_19::varchar AS "vendorID", var_20 AS "viz" on conflict do nothing on conflict ("true") do update set "image0ID" = var_21,"note0" = var_22,"quantity" = var_23,"steptag" = var_24,"title" = var_25,"viz" = var_26 returning "crTC", "handle", "id", "idkey", "image0ID", "locID", "note0", "okey", "productID", "purchaseID", "purchasingID", "quantity", "srcID", "steptag", "title", "tkvTC", "typename", "upTC", "vendorID", "viz"

to debugging this:

DB problem 'syntax error at or near "on"' sending query:
  INSERT INTO "ndd" (
    "crTC", "handle", "id", "idkey", "image0ID", "locID", "note0", "okey", "productID", "purchaseID", "purchasingID", "quantity", "srcID", "steptag", "title", "tkvTC", "typename", "upTC", "vendorID", "viz"
  )
  select 'val_1'::varchar AS "crTC",
     'val_2'::varchar AS "handle",
     'val_3'::varchar AS "id",
     'val_4'::varchar AS "idkey",
     'val_5'::varchar AS "image0ID",
     'val_6'::varchar AS "locID",
     'val_7'::varchar AS "note0",
     'val_8'::varchar AS "okey",
     'val_9'::varchar AS "productID",
     'val_10'::varchar AS "purchaseID",
     'val_11'::varchar AS "purchasingID",
     12::integer AS "quantity",
     'val_13'::varchar AS "srcID",
     'val_14' AS "steptag",
     'val_15'::varchar AS "title",
     'val_16'::varchar AS "tkvTC",
     'val_17'::varchar AS "typename",
     'val_18'::varchar AS "upTC",
     'val_19'::varchar AS "vendorID",
     'val_20' AS "viz"
  on 
-- ^^^ Parser cannot proceed: Unexpected token at 1:842
conflict do nothing on conflict ("true") do update set "image0ID" = var_21,"note0" = var_22,"quantity" = var_23,"steptag" = var_24,"title" = var_25,"viz" = var_26 returning "crTC", "handle", "id", "idkey", "image0ID", "locID", "note0", "okey", "productID", "purchaseID", "purchasingID", "quantity", "srcID", "steptag", "title", "tkvTC", "typename", "upTC", "vendorID", "viz"

If that's too aspirational, is there a way to have the plugin snip out the context around where it failed? The plugin error as it stands is Unexpected token at 1:842, but this would be much more pleasant:

Unexpected token at 1:842, near this segment:
        al_20' AS "viz" on conflict do nothing on conflict ("true") do update set "image
                                              ^^^

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions