11# lib/utils.py
2+ import click
3+ from enum import Enum
24import time
35import json
46import sys
57import subprocess
68import re
79
10+ class BorderColor (Enum ):
11+ YELLOW = '\033 [93m' # Bright yellow
12+ BLUE = '\033 [94m' # Bright blue
13+ RED = '\033 [91m' # Bright red
14+
15+ def print_unicode_box (message : str , color : BorderColor = BorderColor .YELLOW ):
16+ border_color = color .value
17+ reset_color = '\033 [0m'
18+
19+ lines = message .split ('\n ' )
20+ max_length = max (len (line ) for line in lines )
21+ top_border = border_color + '┌' + '─' * (max_length + 2 ) + '┐' + reset_color
22+ bottom_border = border_color + '└' + '─' * (max_length + 2 ) + '┘' + reset_color
23+
24+ click .echo (top_border )
25+ for line in lines :
26+ click .echo (border_color + '│ ' + line .ljust (max_length ) + ' │' + reset_color )
27+ click .echo (bottom_border )
28+
829def catch_error_and_exit (errmsg , logger ):
930 logger .error (errmsg )
1031 sys .exit ("stackql-deploy operation failed 🚫" )
@@ -18,6 +39,7 @@ def get_type(resource, logger):
1839
1940def run_stackql_query (query , stackql , suppress_errors , logger , custom_auth = None , env_vars = None , retries = 0 , delay = 5 ):
2041 attempt = 0
42+ last_error = None
2143 while attempt <= retries :
2244 try :
2345 logger .debug (f"(utils.run_stackql_query) executing stackql query on attempt { attempt + 1 } :\n \n { query } \n " )
@@ -29,20 +51,22 @@ def run_stackql_query(query, stackql, suppress_errors, logger, custom_auth=None,
2951 if len (result ) == 0 :
3052 logger .debug ("(utils.run_stackql_query) stackql query executed successfully, retrieved 0 items." )
3153 pass
32- elif not suppress_errors and result and 'error' in result [0 ]:
54+ elif result and 'error' in result [0 ]:
3355 error_message = result [0 ]['error' ]
34- if attempt == retries :
35- # If retries are exhausted, log the error and exit
36- catch_error_and_exit (
37- (
38- f"(utils.run_stackql_query) error occurred during stackql query execution:\n \n "
39- f"{ error_message } \n "
40- ),
41- logger
42- )
43- else :
44- # Log the error and prepare for another attempt
45- logger .error (f"attempt { attempt + 1 } failed:\n \n { error_message } \n " )
56+ last_error = error_message # Store the error for potential return
57+ if not suppress_errors :
58+ if attempt == retries :
59+ # If retries are exhausted, log the error and exit
60+ catch_error_and_exit (
61+ (
62+ f"(utils.run_stackql_query) error occurred during stackql query execution:\n \n "
63+ f"{ error_message } \n "
64+ ),
65+ logger
66+ )
67+ else :
68+ # Log the error and prepare for another attempt
69+ logger .error (f"attempt { attempt + 1 } failed:\n \n { error_message } \n " )
4670 elif 'count' in result [0 ]:
4771 # If the result is a count query, return the count
4872 logger .debug (
@@ -74,6 +98,7 @@ def run_stackql_query(query, stackql, suppress_errors, logger, custom_auth=None,
7498
7599 except Exception as e :
76100 # Log the exception and check if retry attempts are exhausted
101+ last_error = str (e ) # Store the exception for potential return
77102 if attempt == retries :
78103 catch_error_and_exit (
79104 f"(utils.run_stackql_query) an exception occurred during stackql query execution:\n \n { str (e )} \n " ,
@@ -87,6 +112,9 @@ def run_stackql_query(query, stackql, suppress_errors, logger, custom_auth=None,
87112 attempt += 1
88113
89114 logger .debug (f"(utils.run_stackql_query) all attempts ({ retries + 1 } ) to execute the query completed." )
115+ # If suppress_errors is True and we have an error, return an empty list with error info as a special dict
116+ if suppress_errors and last_error :
117+ return [{'_stackql_deploy_error' : last_error }]
90118 # return None
91119 return []
92120
0 commit comments