@@ -242,7 +242,11 @@ def _create_verbose_err_msg(self, error_input: Any) -> Union[Dict[str, Any], str
242242 return str (error_input ) # Fallback to string representation
243243
244244 def create_err_msg (
245- self , err_type : str , err_msg : str , error_obj : Optional [Exception ] = None
245+ self ,
246+ err_type : str ,
247+ err_msg : str ,
248+ error_obj : Optional [Exception ] = None ,
249+ schema_uri : str = "" ,
246250 ) -> Dict [str , Union [str , bool , List [str ], Dict [str , Any ]]]:
247251 """
248252 Create a standardized error message dictionary and mark validation as failed.
@@ -251,26 +255,59 @@ def create_err_msg(
251255 err_type (str): The type of error.
252256 err_msg (str): The error message.
253257 error_obj (Optional[Exception]): The raw exception object for verbose details.
258+ schema_uri (str, optional): The URI of the schema that failed validation.
254259
255260 Returns:
256261 dict: Dictionary containing error information.
257262 """
258263 self .valid = False
259264
260265 # Ensure all values are of the correct type
261- version_str : str = str (self .version ) if self .version is not None else ""
262- path_str : str = str (self .stac_file ) if self .stac_file is not None else ""
266+ if not isinstance (err_type , str ):
267+ err_type = str (err_type )
268+ if not isinstance (err_msg , str ):
269+ err_msg = str (err_msg )
270+
271+ # Initialize the message with common fields
272+ message : Dict [str , Any ] = {
273+ "version" : str (self .version ) if hasattr (self , "version" ) else "" ,
274+ "path" : str (self .stac_file ) if hasattr (self , "stac_file" ) else "" ,
275+ "schema" : (
276+ [self ._original_schema_paths .get (self .schema , self .schema )]
277+ if hasattr (self , "schema" )
278+ else ["" ]
279+ ),
280+ "valid_stac" : False ,
281+ "error_type" : err_type ,
282+ "error_message" : err_msg ,
283+ "failed_schema" : schema_uri if schema_uri else "" ,
284+ "recommendation" : "For more accurate error information, rerun with --verbose." ,
285+ }
286+
287+ # Add asset_type and validation_method if available
288+ if hasattr (self , "stac_content" ):
289+ try :
290+ stac_type = get_stac_type (self .stac_content )
291+ if stac_type :
292+ message ["asset_type" ] = stac_type .upper ()
293+ message ["validation_method" ] = (
294+ "recursive"
295+ if hasattr (self , "recursive" ) and self .recursive
296+ else "default"
297+ )
298+ except Exception : # noqa: BLE001
299+ pass
263300
264301 # Ensure schema is properly typed
265302 schema_value : str = ""
266303 if self .schema is not None :
267304 schema_value = str (self .schema )
268305 schema_field : List [str ] = [schema_value ] if schema_value else []
269306
270- # Initialize the message with common fields
271- message : Dict [str , Union [str , bool , List [str ], Dict [str , Any ]]] = {
272- "version" : version_str ,
273- "path" : path_str ,
307+ # Initialize the error message with common fields
308+ error_message : Dict [str , Union [str , bool , List [str ], Dict [str , Any ]]] = {
309+ "version" : str ( self . version ) if self . version is not None else "" ,
310+ "path" : str ( self . stac_file ) if self . stac_file is not None else "" ,
274311 "schema" : schema_field , # All schemas that were checked
275312 "valid_stac" : False ,
276313 "error_type" : err_type ,
@@ -281,31 +318,31 @@ def create_err_msg(
281318 # Try to extract the failed schema from the error message if it's a validation error
282319 if error_obj and hasattr (error_obj , "schema" ):
283320 if isinstance (error_obj .schema , dict ) and "$id" in error_obj .schema :
284- message ["failed_schema" ] = error_obj .schema ["$id" ]
321+ error_message ["failed_schema" ] = error_obj .schema ["$id" ]
285322 elif hasattr (error_obj , "schema_url" ):
286- message ["failed_schema" ] = error_obj .schema_url
323+ error_message ["failed_schema" ] = error_obj .schema_url
287324 # If we can't find a schema ID, try to get it from the schema map
288325 elif schema_field and len (schema_field ) == 1 :
289- message ["failed_schema" ] = schema_field [0 ]
326+ error_message ["failed_schema" ] = schema_field [0 ]
290327
291328 if self .verbose and error_obj is not None :
292329 verbose_err = self ._create_verbose_err_msg (error_obj )
293330 if isinstance (verbose_err , dict ):
294- message ["error_verbose" ] = verbose_err
331+ error_message ["error_verbose" ] = verbose_err
295332 else :
296- message ["error_verbose" ] = {"detail" : str (verbose_err )}
333+ error_message ["error_verbose" ] = {"detail" : str (verbose_err )}
297334 # Add recommendation to check the schema if the error is not clear
298- if "failed_schema" in message and message [ "failed_schema" ] :
299- message ["recommendation" ] = (
335+ if error_message . get ( "failed_schema" ) :
336+ error_message ["recommendation" ] = (
300337 "If the error is unclear, please check the schema documentation at: "
301- f"{ message ['failed_schema' ]} "
338+ f"{ error_message ['failed_schema' ]} "
302339 )
303340 else :
304- message ["recommendation" ] = (
341+ error_message ["recommendation" ] = (
305342 "For more accurate error information, rerun with --verbose."
306343 )
307344
308- return message
345+ return error_message
309346
310347 def create_links_message (self ) -> Dict :
311348 """
@@ -604,7 +641,9 @@ def recursive_validator(self, stac_type: str) -> bool:
604641 err_type = "JSONSchemaValidationError" ,
605642 err_msg = err_msg ,
606643 error_obj = e ,
607- schema_uri = e .schema .get ("$id" , "" ) if hasattr (e , "schema" ) else "" ,
644+ schema_uri = (
645+ e .schema .get ("$id" , "" ) if hasattr (e , "schema" ) else ""
646+ ),
608647 )
609648 )
610649 self .message .append (message )
@@ -686,7 +725,9 @@ def recursive_validator(self, stac_type: str) -> bool:
686725 if link ["rel" ] == "item" :
687726 self .schema = set_schema_addr (self .version , stac_type .lower ())
688727 message = self .create_message (stac_type , "recursive" )
689- message ["validator_engine" ] = "pydantic" if self .pydantic else "jsonschema"
728+ message ["validator_engine" ] = (
729+ "pydantic" if self .pydantic else "jsonschema"
730+ )
690731 try :
691732 if self .pydantic :
692733 # Set pydantic model info in schema field for child items
0 commit comments