@@ -313,8 +313,10 @@ def _load_external_models(
313313 for model in external_models :
314314 if model .gateway is None :
315315 if model .fqn in models :
316- self ._raise_failed_to_load_model_error (
317- path , f"Duplicate external model name: '{ model .name } '."
316+ raise ConfigError (
317+ self ._failed_to_load_model_error (
318+ path , f"Duplicate external model name: '{ model .name } '."
319+ )
318320 )
319321 models [model .fqn ] = model
320322
@@ -323,8 +325,10 @@ def _load_external_models(
323325 for model in external_models :
324326 if model .gateway == gateway :
325327 if model .fqn in models and models [model .fqn ].gateway == gateway :
326- self ._raise_failed_to_load_model_error (
327- path , f"Duplicate external model name: '{ model .name } '."
328+ raise ConfigError (
329+ self ._failed_to_load_model_error (
330+ path , f"Duplicate external model name: '{ model .name } '."
331+ )
328332 )
329333 models .update ({model .fqn : model })
330334
@@ -411,11 +415,12 @@ def _track_file(self, path: Path) -> None:
411415 """Project file to track for modifications"""
412416 self ._path_mtimes [path ] = path .stat ().st_mtime
413417
414- def _raise_failed_to_load_model_error (self , path : Path , error : t .Union [str , Exception ]) -> None :
418+ def _failed_to_load_model_error (self , path : Path , error : t .Union [str , Exception ]) -> str :
415419 base_message = f"Failed to load model definition at '{ path } ':"
416420 if isinstance (error , ValidationError ):
417- raise ConfigError (validation_error_message (error , base_message ))
418- raise ConfigError (f"{ base_message } \n { error } " )
421+ return validation_error_message (error , base_message )
422+ return f"{ base_message } \n { error } "
423+
419424
420425class SqlMeshLoader (Loader ):
421426 """Loads macros and models for a context using the SQLMesh file formats"""
@@ -552,17 +557,18 @@ def _load_sql_models(
552557 path = futures_to_paths [future ]
553558 try :
554559 _ , loaded = future .result ()
555- if loaded :
556- for model in loaded :
557- if model .enabled :
558- model ._path = path
559- models [model .fqn ] = model
560- else :
561- for model in cache .get (path ):
562- if model .enabled :
563- models [model .fqn ] = model
560+ for model in loaded or cache .get (path ):
561+ if model .fqn in models :
562+ errors .append (
563+ self ._failed_to_load_model_error (
564+ path , f"Duplicate SQL model name: '{ model .name } '."
565+ )
566+ )
567+ elif model .enabled :
568+ model ._path = path
569+ models [model .fqn ] = model
564570 except Exception as ex :
565- errors .append (f"Failed to load model definition at ' { path } '. \n \n { ex } " )
571+ errors .append (self . _failed_to_load_model_error ( path , str ( ex )) )
566572
567573 if errors :
568574 error_string = "\n " .join (errors )
@@ -618,7 +624,7 @@ def _load_python_models(
618624 if model .enabled :
619625 models [model .fqn ] = model
620626 except Exception as ex :
621- self ._raise_failed_to_load_model_error (path , ex )
627+ raise ConfigError ( self ._failed_to_load_model_error (path , ex ) )
622628
623629 finally :
624630 model_registry ._dialect = None
0 commit comments