7
7
import traceback
8
8
from typing import Dict , Tuple , Union
9
9
10
- from colorama import Fore , init
10
+ from colorama import Fore , init , Style
11
11
from marshmallow .exceptions import ValidationError as SchemaValidationError
12
12
13
13
from azure .ai .ml .constants ._common import (
@@ -86,7 +86,7 @@ def format_details_section(
86
86
87
87
if hasattr (error , "message" ):
88
88
error_types [error .error_type ] = True
89
- details += f"\n \n { error .message } \n "
89
+ details += f"\n \n { Fore . RED } (x) { error .message } { Fore . RESET } \n "
90
90
else :
91
91
if (
92
92
entity_type == ErrorTarget .COMMAND_JOB
@@ -139,12 +139,15 @@ def format_details_section(
139
139
if isinstance (field_error , dict ):
140
140
field_error = f"{ list (field_error .keys ())[0 ]} :\n - { list (field_error .values ())[0 ][0 ]} "
141
141
142
- details += f"\n { field } :\n - { field_error } \n "
143
-
142
+ details += f"{ Fore .RED } \n (x) { field } :\n - { field_error } { Fore .RESET } \n "
144
143
return error_types , details
145
144
146
145
147
- def format_errors_and_resolutions_sections (entity_type : str , error_types : Dict [str , bool ]) -> Tuple [str , str ]:
146
+ def format_errors_and_resolutions_sections (
147
+ entity_type : str ,
148
+ error_types : Dict [str , bool ],
149
+ cli : bool
150
+ ) -> Tuple [str , str ]:
148
151
"""Builds strings for details of the error message template's Errors and Resolutions sections."""
149
152
150
153
resolutions = ""
@@ -153,40 +156,50 @@ def format_errors_and_resolutions_sections(entity_type: str, error_types: Dict[s
153
156
154
157
if error_types [ValidationErrorType .INVALID_VALUE ]:
155
158
errors += f"\n { count } ) One or more fields are invalid"
156
- resolutions += f"Double-check that all specified parameters are of the correct types and formats \
157
- prescribed by the { entity_type } schema."
159
+ resolutions += f"\n { count } ) Double-check that all specified parameters are of the correct types and formats " \
160
+ f" prescribed by the { entity_type } schema."
158
161
count += 1
159
162
if error_types [ValidationErrorType .UNKNOWN_FIELD ]:
160
163
errors += f"\n { count } ) A least one unrecognized parameter is specified"
161
- resolutions += f"Remove any parameters not prescribed by the { entity_type } schema."
164
+ resolutions += f"\n { count } ) Remove any parameters not prescribed by the { entity_type } schema."
162
165
count += 1
163
166
if error_types [ValidationErrorType .MISSING_FIELD ]:
164
167
errors += f"\n { count } ) At least one required parameter is missing"
165
- resolutions += f"Ensure all parameters required by the { entity_type } schema are specified."
168
+ resolutions += f"\n { count } ) Ensure all parameters required by the { entity_type } schema are specified."
166
169
count += 1
167
170
if error_types [ValidationErrorType .FILE_OR_FOLDER_NOT_FOUND ]:
168
171
errors += f"\n { count } ) One or more files or folders do not exist.\n "
169
- resolutions += " Double-check the directory paths you provided and enter the correct paths ."
172
+ resolutions += f" \n { count } ) Double-check the directory path you provided and enter the correct path ."
170
173
count += 1
171
174
if error_types [ValidationErrorType .CANNOT_SERIALIZE ]:
172
175
errors += f"\n { count } ) One or more fields cannot be serialized.\n "
173
- resolutions += f"Double-check that all specified parameters are of the correct types and formats \
176
+ resolutions += f"\n { count } ) Double-check that all specified parameters are of the correct types and formats \
174
177
prescribed by the { entity_type } schema."
175
178
count += 1
176
179
if error_types [ValidationErrorType .CANNOT_PARSE ]:
177
180
errors += f"\n { count } ) YAML file cannot be parsed.\n "
178
- resolutions += " Double-check your YAML file for syntax and formatting errors."
181
+ resolutions += f" \n { count } ) Double-check your YAML file for syntax and formatting errors."
179
182
count += 1
180
183
if error_types [ValidationErrorType .RESOURCE_NOT_FOUND ]:
181
184
errors += f"\n { count } ) Resource was not found.\n "
182
- resolutions += "Double-check that the resource has been specified correctly and that you have access to it."
185
+ resolutions += f"\n { count } ) Double-check that the resource has been specified correctly and " \
186
+ "that you have access to it."
183
187
count += 1
184
188
189
+ if cli :
190
+ errors = "Error: " + errors
191
+ else :
192
+ errors = Fore .BLACK + errors + Fore .RESET
193
+
185
194
return errors , resolutions
186
195
187
196
188
197
def format_create_validation_error (
189
- error : Union [SchemaValidationError , ValidationException ], yaml_operation : bool
198
+ error : Union [SchemaValidationError ,
199
+ ValidationException ],
200
+ yaml_operation : bool ,
201
+ cli : bool = False ,
202
+ raw_error : str = None ,
190
203
) -> str :
191
204
"""
192
205
Formats a detailed error message for validation errors.
@@ -204,12 +217,15 @@ def format_create_validation_error(
204
217
from azure .ai .ml ._schema .assets .environment import EnvironmentSchema
205
218
from azure .ai .ml ._schema .assets .model import ModelSchema
206
219
220
+ if raw_error :
221
+ error = raw_error
207
222
entity_type , details = get_entity_type (error )
208
223
error_types , details = format_details_section (error , details , entity_type )
209
- errors , resolutions = format_errors_and_resolutions_sections (entity_type , error_types )
224
+ errors , resolutions = format_errors_and_resolutions_sections (entity_type , error_types , cli )
210
225
211
226
if yaml_operation :
212
227
description = YAML_CREATION_ERROR_DESCRIPTION .format (entity_type = entity_type )
228
+ description = Style .BRIGHT + description + Style .RESET_ALL
213
229
214
230
if entity_type == ErrorTarget .MODEL :
215
231
schema_type = ModelSchema
@@ -242,9 +258,11 @@ def format_create_validation_error(
242
258
error_msg = errors ,
243
259
parsed_error_details = details ,
244
260
resolutions = resolutions ,
261
+ text_color = Fore .WHITE ,
262
+ link_color = Fore .CYAN ,
263
+ reset = Fore .RESET
245
264
)
246
265
247
- formatted_error = Fore .RED + formatted_error + Fore .RESET
248
266
return formatted_error
249
267
250
268
@@ -265,10 +283,16 @@ def log_and_raise_error(error, debug=False, yaml_operation=False):
265
283
elif isinstance (error , ValidationException ):
266
284
module_logger .debug (traceback .format_exc ())
267
285
try :
268
- if error .error_type == ValidationErrorType .GENERIC :
286
+ error_type = error .error_type
287
+ if error_type == ValidationErrorType .GENERIC :
269
288
formatted_error = error
270
289
else :
271
290
formatted_error = format_create_validation_error (error , yaml_operation = yaml_operation )
291
+ raise ValidationException (
292
+ message = formatted_error ,
293
+ no_personal_data_message = "" ,
294
+ error_type = error_type ,
295
+ )
272
296
except NotImplementedError :
273
297
formatted_error = error
274
298
else :
0 commit comments