feat: print template line numbers for errors when rendering ERB

Aperta
#707 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
45/100
Tipo di issue
Funzionalità
Chiarezza
Specificata chiaramente
Stato di attività
Ferma
Stack tecnologico
ruby
Ambito
cli

Direzione di ricerca

Start in lib/krane/renderer.rb, where the three template-rendering errors are re-raised, and lib/krane/render_task.rb, where the error message is assembled. Trace how exception backtraces and content reach the logger, then verify that errors show the original stacktrace and numbered merged template content.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Feature request

  • If the maintainers agree with the feature as described here, I intend to submit a Pull Request myself.1 (I would, because I already have the patch described locally, but this is a minor fix and I'd need to wait for our legal dept to clear the CLA)

Proposal: [provide details on the behaviour you'd like to see and why it would be useful]

When there is an error in the values interpolated into the templates, or an error with the partials, the message is confusing to developers because it does not indicate where the error occurred: eg:

[FATAL][2020-03-17 16:47:02 +0530]	Invalid template: deployment.yml.erb
[FATAL][2020-03-17 16:47:02 +0530]	> Error message:
[FATAL][2020-03-17 16:47:02 +0530]	    undefined method `[]' for nil:NilClass

(followed by a 300 line template). This happens for 3 reasons:

  1. the stacktrace isn't printed as part of the error;
  2. it turns out the stacktraces that are collected are not useful because exceptions are re-thrown without their original context;
  3. to understand the line number from the erb, you need to know the line numbering of the merged template, not the numbering from the file on disk.

Point (2): in lib/krane/renderer.rb, there are 3 lines where an error is caught and thrown without its context. In each case a line like:

raise InvalidTemplateError.new(err.message, filename: filename, content: raw_template)

should be replaced with a line like:

raise InvalidTemplateError.new(err.message, filename: filename, content: raw_template), err.message, err.backtrace

Next point (1): in lib/krane/render_task.rb, to print this, we need something like:

debug_msg += "> Stacktrace:\n#{FormattedLogger.indent_four(exception.backtrace.join("\n"))}"

(depending where you print this you will need to add other newlines to debug_msg).
This together with the fix for (2) means we get traces like:

[FATAL][2020-03-17 12:24:46 +0000]	> Stacktrace:
[FATAL][2020-03-17 12:24:46 +0000]	    (erb):80:in `template_binding'
[FATAL][2020-03-17 12:24:46 +0000]	    /Users/bewins/.rbenv/versions/2.5.3/lib/ruby/2.5.0/erb.rb:876:in `eval'

Finally to understand what line 80 means in the above, we need to show the numbering of the template. One possible implementation is below:

        numbered = exception.content.lines.map.with_index {|item, index| sprintf("% 4d: %s", index + 1, item)}
        debug_msg += "\n> Template content:\n#{FormattedLogger.indent_four(numbered).join("")}"

1 This is the quickest way to get a new feature! We reserve the right to close feature requests, even ones we like, if the proposer does not intend to contribute to the feature and it doesn't fit in our current roadmap.

Lingua principale
Ruby
Stelle
1.4k
Fork
119
Merge medio
1h 7m
PR unite (30g)
1

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di Shopify/krane

Tutte le issue di Shopify/krane

Issue simili

Altre issue su Ruby

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.