From ad02d6f46bdb837dcd633950aa3dca980964085f Mon Sep 17 00:00:00 2001 From: akadusei Date: Wed, 29 Apr 2026 09:04:14 +0000 Subject: [PATCH] Raise compile errors on relevant type node --- src/avram/callbacks/after_commit_callback.cr | 2 +- src/avram/callbacks/callbacks.cr | 2 +- src/avram/callbacks/delete_callbacks.cr | 2 +- src/avram/callbacks/save_callbacks.cr | 2 +- src/avram/define_attribute.cr | 5 +++-- src/avram/migrator/alter_table_statement.cr | 11 +++++------ src/avram/migrator/create_table_statement.cr | 4 ++-- src/avram/model.cr | 2 +- src/avram/nested_save_operation.cr | 2 +- src/avram/save_operation.cr | 6 +++--- 10 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/avram/callbacks/after_commit_callback.cr b/src/avram/callbacks/after_commit_callback.cr index e5fccda62..082b31173 100644 --- a/src/avram/callbacks/after_commit_callback.cr +++ b/src/avram/callbacks/after_commit_callback.cr @@ -56,7 +56,7 @@ module Avram::AfterCommitCallback {% end %} {% if block.args.size != 1 - raise <<-ERR + block.raise <<-ERR The 'after_commit' callback requires exactly 1 block arg to be passed. Example: after_commit do |saved_user| diff --git a/src/avram/callbacks/callbacks.cr b/src/avram/callbacks/callbacks.cr index afa1f9279..68eff3306 100644 --- a/src/avram/callbacks/callbacks.cr +++ b/src/avram/callbacks/callbacks.cr @@ -107,7 +107,7 @@ module Avram::Callbacks macro after_run(&block) {% if block.args.size != 1 - raise <<-ERR + block.raise <<-ERR The 'after_run' callback requires exactly 1 block arg to be passed. Example: after_run { |value| some_method(value) } diff --git a/src/avram/callbacks/delete_callbacks.cr b/src/avram/callbacks/delete_callbacks.cr index f434fd539..a18025b5c 100644 --- a/src/avram/callbacks/delete_callbacks.cr +++ b/src/avram/callbacks/delete_callbacks.cr @@ -73,7 +73,7 @@ module Avram::DeleteCallbacks {% end %} {% if block.args.size != 1 - raise <<-ERR + block.raise <<-ERR The 'after_delete' callback requires exactly 1 block arg to be passed. Example: after_delete do |deleted_user| diff --git a/src/avram/callbacks/save_callbacks.cr b/src/avram/callbacks/save_callbacks.cr index e15734ab2..36a085fa2 100644 --- a/src/avram/callbacks/save_callbacks.cr +++ b/src/avram/callbacks/save_callbacks.cr @@ -164,7 +164,7 @@ module Avram::SaveCallbacks {% end %} {% if block.args.size != 1 - raise <<-ERR + block.raise <<-ERR The 'after_save' callback requires exactly 1 block arg to be passed. Example: after_save do |saved_user| diff --git a/src/avram/define_attribute.cr b/src/avram/define_attribute.cr index 8f04d6f84..70005f5de 100644 --- a/src/avram/define_attribute.cr +++ b/src/avram/define_attribute.cr @@ -40,7 +40,8 @@ module Avram::DefineAttribute {% else %} {% default_value = "= #{type_declaration.value}" %} {% end %} - {% raise <<-ERROR + + {% type_declaration.type.raise <<-ERROR `attribute` in #{@type} must not be called with a type union or nilable type but was called with #{type_declaration.type} If you were attempting to create a nilable attribute, all attributes are considered nilable by default. @@ -89,7 +90,7 @@ module Avram::DefineAttribute macro file_attribute(key) {% unless key.is_a?(SymbolLiteral) %} - {% raise "file_attribute must be declared with a Symbol" %} + {% key.raise "file_attribute must be declared with a Symbol" %} {% end %} attribute {{ key.id }} : Avram::Uploadable diff --git a/src/avram/migrator/alter_table_statement.cr b/src/avram/migrator/alter_table_statement.cr index 7c0fedcfa..fa2bab4e8 100644 --- a/src/avram/migrator/alter_table_statement.cr +++ b/src/avram/migrator/alter_table_statement.cr @@ -29,7 +29,7 @@ class Avram::Migrator::AlterTableStatement macro change_type(type_declaration, **type_options) {% if !type_declaration.is_a?(TypeDeclaration) - raise "Must pass a type declaration to 'change_type'. Example: change_type age : Int32" + type_declaration.raise "Must pass a type declaration to 'change_type'. Example: change_type age : Int32" elsif type_options.keys.includes?("default".id) raise "Cannot change the default value from `change_type`. Use `change_default` instead." end @@ -70,7 +70,7 @@ class Avram::Migrator::AlterTableStatement macro change_default(type_declaration, default) {% if !type_declaration.is_a?(TypeDeclaration) - raise "Must pass a type declaration to 'change_default'. Example: change_default count : Int32, default: 1" + type_declaration.raise "Must pass a type declaration to 'change_default'. Example: change_default count : Int32, default: 1" end %} %column = ::Avram::Migrator::Columns::{{ type_declaration.type }}Column({{ type_declaration.type }}).new( @@ -176,10 +176,10 @@ class Avram::Migrator::AlterTableStatement # Adds a references column and index given a model class and references option. macro add_belongs_to(type_declaration, on_delete, references = nil, foreign_key_type = Int64, fill_existing_with = nil, unique = false, index = true) {% unless type_declaration.is_a?(TypeDeclaration) %} - {% raise "add_belongs_to expected a type declaration like 'user : User', instead got: '#{type_declaration}'" %} + {% type_declaration.raise "add_belongs_to expected a type declaration like 'user : User', instead got: '#{type_declaration}'" %} {% end %} {% if type_declaration.type.stringify =~ /\w::\w/ && references.nil? %} - {% raise <<-ERROR + {% type_declaration.raise <<-ERROR Namespaced models must include the `references` option with the name of the table. Try this... @@ -297,14 +297,13 @@ class Avram::Migrator::AlterTableStatement end macro symbol_expected_error(action, name) - {% if name.is_a?(TypeDeclaration) %} {% example = name.var %} {% else %} {% example = name.id %} {% end %} - {% raise <<-ERROR + {% name.raise <<-ERROR #{action} expected a symbol like :#{example}, instead got: #{name}. diff --git a/src/avram/migrator/create_table_statement.cr b/src/avram/migrator/create_table_statement.cr index 0b5ec35a7..3af5f2886 100644 --- a/src/avram/migrator/create_table_statement.cr +++ b/src/avram/migrator/create_table_statement.cr @@ -161,10 +161,10 @@ class Avram::Migrator::CreateTableStatement # Adds a references column and index given a model class and references option. macro add_belongs_to(type_declaration, on_delete, references = nil, foreign_key_type = Int64, unique = false, index = true) {% unless type_declaration.is_a?(TypeDeclaration) %} - {% raise "add_belongs_to expected a type declaration like 'user : User', instead got: '#{type_declaration}'" %} + {% type_declaration.raise "add_belongs_to expected a type declaration like 'user : User', instead got: '#{type_declaration}'" %} {% end %} {% if type_declaration.type.stringify =~ /\w::\w/ && references.nil? %} - {% raise <<-ERROR + {% type_declaration.raise <<-ERROR Namespaced models must include the `references` option with the name of the table. Try this... diff --git a/src/avram/model.cr b/src/avram/model.cr index 21dcacda9..c99ef3e4e 100644 --- a/src/avram/model.cr +++ b/src/avram/model.cr @@ -133,7 +133,7 @@ abstract class Avram::Model {% value_generator = type_declaration.value %} {% if !value_generator || value_generator && !(value_generator.is_a?(ProcLiteral) || value_generator.is_a?(ProcPointer) || value_generator.is_a?(Call)) %} - {% raise <<-ERROR + {% type_declaration.type.raise <<-ERROR When using a String primary_key, you must also specify a way to generate the value. You can provide a class method, a proc or a proc pointer. Your value generator must return a non-nullable String. diff --git a/src/avram/nested_save_operation.cr b/src/avram/nested_save_operation.cr index 56d9cc49f..35e342eee 100644 --- a/src/avram/nested_save_operation.cr +++ b/src/avram/nested_save_operation.cr @@ -13,7 +13,7 @@ module Avram::NestedSaveOperation end %} {% unless assoc %} - {% raise "#{T} must have a has_one association with #{model_type}" %} + {% type_declaration.raise "#{T} must have a has_one association with #{model_type}" %} {% end %} @_{{ name }} : {{ type }} | Nil diff --git a/src/avram/save_operation.cr b/src/avram/save_operation.cr index cd56f2a1b..ac2e33034 100644 --- a/src/avram/save_operation.cr +++ b/src/avram/save_operation.cr @@ -152,7 +152,7 @@ abstract class Avram::SaveOperation(T) macro permit_columns(*attribute_names) {% for attribute_name in attribute_names %} {% if attribute_name.is_a?(TypeDeclaration) %} - {% raise <<-ERROR + {% attribute_name.raise <<-ERROR Must use a Symbol or a bare word in 'permit_columns'. Instead, got: #{attribute_name} Try this... @@ -163,7 +163,7 @@ abstract class Avram::SaveOperation(T) %} {% end %} {% unless attribute_name.is_a?(SymbolLiteral) || attribute_name.is_a?(Call) %} - {% raise <<-ERROR + {% attribute_name.raise <<-ERROR Must use a Symbol or a bare word in 'permit_columns'. Instead, got: #{attribute_name} Try this... @@ -181,7 +181,7 @@ abstract class Avram::SaveOperation(T) @@permitted_param_keys << "{{ attribute_name.id }}" {% else %} - {% raise <<-ERROR + {% attribute_name.raise <<-ERROR Can't permit '#{attribute_name}' because the column has not been defined on the model for #{@type}. Try this...