Knowledge Drop

General Code & LookML Troubleshooting Steps

  • 8 July 2021
  • 0 replies
  • 14 views

Userlevel 4

Last tested: Mar 29, 2019
 

Problem

My code is throwing an error, and I don't know what part of the code is throwing the error! This can be:

  • LookML
  • An API script
  • Complex Table Calculations
  • Any code at all!

Troubleshooting Approach

Generally speaking, there are some steps you can take to troubleshoot code regardless of what kind of code it is. Simply put: Cut your code or logical complexity in-half, and check if the problem goes away. Keep halving your problem until you've pinpointed it! When you know where the problem is, the solution often becomes clear.

Break your problem down into smaller logical pieces in place of complicated nested calculations, if necessary. Then, temporarily comment out code blocks, hardcode values in place of references, disable features, etc, until what remains 'works' (i.e. executes without failing).

Hardcoding Values or Disabling Features

Replace component functions/references with expected/possible 'dummy' values (or create test versions of them). If the error goes away, then that component contains the problem. Repeat the process by simplifying/replacing a subcomponent within the (now smaller) subset of your code that contains the problem.

Example simplifying by swapping in Hardcoded values and/or simpler logic

image.png

Code Comments

We often take advantage of code comments here, which prevent code from being executed or validated. You can use commenting to narrow down where the problem is using a method called binary testing -

  1. Comment out half of the erroneous code. The goal is to comment out all code which isn't causing a problem, until you're left with just the code which contains an error.
    1. In LookML, you can use "#" to comment a line of code.
      1. If you highlight a selection of LookML, you can comment it all at once with "command + /".
      2. If you fold the LookML, then you can use the "command + /" trick to comment out entire blocks of LookML very quickly!
        how to fold.png

        how to comment.png
  2. If the error hasn't disappeared, then again comment half of the erroneous code.
    1. If the error has disappeared, you know that the half you just commented does contain the error! So, uncomment half of the erroneous code and repeat until you get the error to appear again.
      1. In LookML, you can use "command + /" to uncomment commented code!
  3. Repeat step 2 until you've found the line(s) of code which cause the error! Think about what that erroneous code is actually doing and why it may be giving that error message.
    1. In programming languages, printing output like variable names usually helps!

An Example

I'm writing some LookML and just tried to save. I got the error: Invalid lookml syntax near line 41 and I have no idea why.

1.png

  1. First I try to comment out the entire explore. That gets rid of the error!
    2.png
  2. Then, I uncomment the explore-level parameters, to see if it's a problem with the larger explore or the joins. The code still has no errors, so I begin to suspect the joins.
    3.png
  3. There are 4 joins, so I uncomment half of them. I notice that the latter 2 joins reference the first 2 joins, and I don't want to cause any extra reference errors, so I decide to uncomment the first 2 joins. These still have no error, so now I suspect the two remaining commented joins!
    4.png
  4. Now I decide to uncomment half of the remaining joins. Once I uncomment the join to products , the error returns! So this tells me I should look closer at that join.
    5.png
  5. Now that I'm looking closely at the join to products, I notice that there are two closing brackets for that join! One on line 30 and one on line 34. Silly me. I remove the one on line 30 since I don't want the join definition to end on line 30. Now there are no errors!
    6.png
  6. Just to make sure I found the only error, I uncomment the rest of the code. It also has no errors so we are good to go!
    7.png

Mini-Rant: Modular Coding

This kind of situation is commonly encountered by coders who write a ton of code all at once and then see if it works. You can avoid this kind of problem by writing small bits of code and frequently checking to see if it works. (This is sometimes referred to as Modular Coding.)

In LookML, this means:

  • Save, validate, and commit more often than you think you need to. You'll be more likely to catch the problem early, and the string of commits will help your future self better understand the flow of what you wrote.
  • Test your LookML in the explore page frequently. Anytime you change a LookML parameter beginning with "sql" (sql , sql_on , sql_always_where , etc), go test the change in an explore. The LookML validator doesn't generally check raw SQL in these parameters, so it's important to run the explore page to make sure the database accepts what you've written!

 

This content is subject to limited support.                

 

 


0 replies

Be the first to reply!

Reply