Skip to content

Commit d88d157

Browse files
committed
check for path values before indexing into objects
1 parent f605d60 commit d88d157

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

dbt_docs_to_notion.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,17 @@ def make_request(endpoint, querystring='', method='GET', **request_kwargs):
2929

3030
return resp.json()
3131

32+
33+
def get_path_or_empty(parent_object, path_array, zero_value=''):
34+
obj = parent_object
35+
for el in path_array:
36+
if el not in obj:
37+
return zero_value
38+
obj = obj[el]
39+
40+
return obj
41+
42+
3243
def main():
3344
model_records_to_write = sys.argv[1:] # 'all' or list of model names
3445
print(f'Model records to write: {model_records_to_write}')
@@ -163,7 +174,7 @@ def main():
163174
}
164175
}
165176
]
166-
col_names_and_data = list(catalog_nodes[model_name]['columns'].items())
177+
col_names_and_data = list(get_path_or_empty(catalog_nodes, [model_name, 'columns'], {}).items())
167178
for (col_name, col_data) in col_names_and_data[:98]: # notion api limit is 100 table rows
168179
columns_table_children_obj.append(
169180
{
@@ -367,7 +378,7 @@ def main():
367378
{
368379
"text": {
369380
"content": str(
370-
catalog_nodes[model_name]['metadata']['owner']
381+
get_path_or_empty(catalog_nodes, [model_name, 'metadata', 'owner'], '')
371382
)[:2000]
372383
}
373384
}
@@ -383,10 +394,10 @@ def main():
383394
]
384395
},
385396
"Approx Rows": {
386-
"number": catalog_nodes[model_name]['stats']['num_rows']['value']
397+
"number": get_path_or_empty(catalog_nodes, [model_name, 'stats', 'num_rows', 'value'], -1)
387398
},
388399
"Approx GB": {
389-
"number": catalog_nodes[model_name]['stats']['num_bytes']['value']/1e9
400+
"number": get_path_or_empty(catalog_nodes, [model_name, 'stats', 'num_bytes', 'value'], -1) /1e9
390401
},
391402
"Depends On": {
392403
"rich_text": [

0 commit comments

Comments
 (0)