mirror of
https://github.com/aljazceru/dev-gpt.git
synced 2025-12-20 23:24:20 +01:00
⛰ fix: prevent large string content in error message
This commit is contained in:
@@ -18,7 +18,7 @@ from jina import Flow
|
||||
|
||||
from dev_gpt.constants import DEMO_TOKEN
|
||||
from dev_gpt.utils.io import suppress_stdout, is_docker_running
|
||||
from dev_gpt.utils.string_tools import print_colored
|
||||
from dev_gpt.utils.string_tools import print_colored, clean_large_words
|
||||
|
||||
|
||||
def wait_until_app_is_ready(url):
|
||||
@@ -309,6 +309,7 @@ def clean_color_codes(response):
|
||||
response = re.sub(r'\x1b\[[0-9;]*m', '', response)
|
||||
return response
|
||||
|
||||
|
||||
def process_error_message(error_message):
|
||||
lines = error_message.split('\n')
|
||||
|
||||
@@ -330,10 +331,12 @@ def process_error_message(error_message):
|
||||
|
||||
response = clean_color_codes(response)
|
||||
|
||||
# the following code makes sure that the error message is cleaned from irrelevant sequences of e.g. base64 strings.
|
||||
response = clean_large_words(response)
|
||||
|
||||
# the following code tests the case that the docker file is corrupted and can not be parsed
|
||||
# the method above will not return a relevant error message in this case
|
||||
# but the last line of the error message will start with "error"
|
||||
|
||||
last_line = lines[-1]
|
||||
if not response and last_line.startswith('error: '):
|
||||
return last_line
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import os
|
||||
import platform
|
||||
import string
|
||||
import re
|
||||
|
||||
if platform.system() == "Windows":
|
||||
os.system("color")
|
||||
@@ -40,3 +41,9 @@ def get_template_parameters(formatted_string):
|
||||
parameters.append(field_name)
|
||||
|
||||
return parameters
|
||||
|
||||
def clean_large_words(text):
|
||||
"""Large words like base64 strings are returned by omitting the middle part of the word."""
|
||||
pattern = r'\b([a-zA-Z0-9+/]{20})([a-zA-Z0-9+/]{200,})([a-zA-Z0-9+/]{20})\b'
|
||||
cleaned_text = re.sub(pattern, r'\1...\3', text)
|
||||
return cleaned_text
|
||||
@@ -1,4 +1,5 @@
|
||||
from dev_gpt.apis.jina_cloud import clean_color_codes
|
||||
from dev_gpt.utils.string_tools import clean_large_words
|
||||
|
||||
|
||||
def test_clean_color_codes():
|
||||
@@ -9,3 +10,14 @@ def test_clean_color_codes():
|
||||
cleaned = clean_color_codes(color)
|
||||
print('with color codes:', color)
|
||||
print('without color codes:', cleaned)
|
||||
|
||||
|
||||
def test_clean_large_words():
|
||||
assert clean_large_words(
|
||||
'''test 2VAzLpbBUDBInhtN5ToJZAXL8L6F4J+Xr/L/42vs2r+9Pb0E3Y1ZLy7E3GsYRzAqQ037iKABMHL9VDoAaBAuAGgQLgBoEC4AaBAuAGgQLgB\
|
||||
oEC4AaBAuAGgQLgBoEC4AaBAuAGgQLgBoEC4AaBAuAGgQLgBoEC4AaBAuAGgQLgBoEC4AaBAuAGgQLgBoEC4AaBAuAGgQLgBoEC4AaBAuAG\
|
||||
gQLgBoEC4AaBAuAGgQLgBoEC4AaBAuAGgQLgBoEC4AaBAuAGgQLgBoEC4AaBAuAGgQLgBoEC4AaBAuAGgQLgBoEC4AaBAuAGgQLgBoEC4Aa\
|
||||
BAuAGgQLgBoEC4AaBAuAGgQLgBoEC4AaBAuAGgQLgBoEC4AaBAuAGgQLgBoEC4AaBAuAGgQLgBoEC4AaBAuAGgQLgBoEC4AaBAuAGgQLgBo test'''
|
||||
) == 'test 2VAzLpbBUDBInhtN5ToJ...LgBoEC4AaBAuAGgQLgBo test'
|
||||
|
||||
assert clean_large_words('2VAzLpbBUDBInhtN5ToJZAXL8L6F4J+Xr/L/4') == '2VAzLpbBUDBInhtN5ToJZAXL8L6F4J+Xr/L/4'
|
||||
|
||||
Reference in New Issue
Block a user