This is an automated email from the ASF dual-hosted git repository.

chaokunyang pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/fory.git


The following commit(s) were added to refs/heads/main by this push:
     new 22bae9730 refactor(dart): merge all runtime exceptions into single 
fory_exception.dart (#3436)
22bae9730 is described below

commit 22bae9730505152f645fe093b807469ab09357b7
Author: Yash Agarwal <[email protected]>
AuthorDate: Sat Feb 28 22:40:38 2026 +0530

    refactor(dart): merge all runtime exceptions into single 
fory_exception.dart (#3436)
    
    ## Why?
    
    The current Dart exception hierarchy spreads runtime exceptions across
    four separate files under `dart/packages/fory/lib/src/exception/`, which
    is overly Java-style for idiomatic Dart.
    
    ## What needs to be done?
    
    - Merge all runtime exception classes from:
      - `exception/**`
      - `codegen/exception/**`
      into a single file: `dart/packages/fory/lib/src/fory_exception.dart`
    
    - Update all imports across serializers, resolvers, context, and codegen
    base to point to the new file.
    - Delete the `exception/` subdirectory and the `codegen/exception/`
    subdirectory.
    
    ## Does this introduce any user-facing change?
    
    - [x] Does this introduce any public API change? (import paths change)
    - [ ] Does this introduce any binary protocol compatibility change?
    
    ## Related
    
    - Requested in #3434 review:
    https://github.com/apache/fory/pull/3434#discussion_r2867271702
---
 .../test/config_test/size_guard_test.dart          |   2 +-
 .../impl/annotation/class_annotation_analyzer.dart |   2 +-
 .../impl/annotation/enum_annotation_analyzer.dart  |   2 +-
 .../impl/annotation/key_annotation_analyzer.dart   |   2 +-
 .../impl/annotation/uint_annotation_analyzer.dart  |   2 +-
 .../impl/constructor/constructor_analyzer.dart     |   2 +-
 .../analyze/impl/field/access_info_analyzer.dart   |   2 +-
 .../analyze/impl/field/field_analyzer_impl.dart    |   2 +-
 .../analyze/impl/struct/custom_type_analyzer.dart  |   2 +-
 .../analyze/impl/type/type_system_analyzer.dart    |   4 +-
 .../codegen/exception/annotation_exception.dart    | 180 -----
 .../exception/constraint_violation_exception.dart  | 210 ------
 .../lib/src/codegen/exception/field_exception.dart |  49 --
 .../codegen/exception/fory_codegen_exception.dart  |  39 --
 .../src/codegen/generate/obj_spec_generator.dart   |   2 +-
 .../src/exception/deserialization_exception.dart   | 149 -----
 .../fory/lib/src/exception/fory_exception.dart     |  42 --
 .../lib/src/exception/registration_exception.dart  | 115 ----
 .../lib/src/exception/serialization_exception.dart |  84 ---
 dart/packages/fory/lib/src/fory_context.dart       |   2 +-
 dart/packages/fory/lib/src/fory_exception.dart     | 728 +++++++++++++++++++++
 .../lib/src/resolver/impl/type_resolver_impl.dart  |   4 +-
 .../fory/lib/src/serializer/array_serializer.dart  |   2 +-
 .../fory/lib/src/serializer/class_serializer.dart  |   2 +-
 .../collection/list/list_serializer.dart           |   2 +-
 .../serializer/collection/map/map_serializer.dart  |   2 +-
 .../serializer/collection/set/set_serializer.dart  |   2 +-
 .../fory/lib/src/serializer/enum_serializer.dart   |   2 +-
 .../fory/lib/src/serializer/string_serializer.dart |   2 +-
 29 files changed, 750 insertions(+), 890 deletions(-)

diff --git a/dart/packages/fory-test/test/config_test/size_guard_test.dart 
b/dart/packages/fory-test/test/config_test/size_guard_test.dart
index a52ed2b56..780c19098 100644
--- a/dart/packages/fory-test/test/config_test/size_guard_test.dart
+++ b/dart/packages/fory-test/test/config_test/size_guard_test.dart
@@ -21,7 +21,7 @@ library;
 
 import 'dart:typed_data';
 import 'package:fory/fory.dart';
-import 'package:fory/src/exception/fory_exception.dart';
+import 'package:fory/src/fory_exception.dart';
 import 'package:test/test.dart';
 
 void main() {
diff --git 
a/dart/packages/fory/lib/src/codegen/analyze/impl/annotation/class_annotation_analyzer.dart
 
b/dart/packages/fory/lib/src/codegen/analyze/impl/annotation/class_annotation_analyzer.dart
index 2fdca3c80..fbb5fbc56 100644
--- 
a/dart/packages/fory/lib/src/codegen/analyze/impl/annotation/class_annotation_analyzer.dart
+++ 
b/dart/packages/fory/lib/src/codegen/analyze/impl/annotation/class_annotation_analyzer.dart
@@ -24,7 +24,7 @@ import 
'package:fory/src/codegen/analyze/analysis_type_identifier.dart';
 import 
'package:fory/src/codegen/analyze/annotation/require_location_level.dart';
 import 'package:fory/src/codegen/const/location_level.dart';
 import 'package:fory/src/codegen/entity/location_mark.dart';
-import 'package:fory/src/codegen/exception/annotation_exception.dart';
+import 'package:fory/src/fory_exception.dart';
 
 class ClassAnnotationAnalyzer {
   const ClassAnnotationAnalyzer();
diff --git 
a/dart/packages/fory/lib/src/codegen/analyze/impl/annotation/enum_annotation_analyzer.dart
 
b/dart/packages/fory/lib/src/codegen/analyze/impl/annotation/enum_annotation_analyzer.dart
index 44be37a18..5dac97bc6 100644
--- 
a/dart/packages/fory/lib/src/codegen/analyze/impl/annotation/enum_annotation_analyzer.dart
+++ 
b/dart/packages/fory/lib/src/codegen/analyze/impl/annotation/enum_annotation_analyzer.dart
@@ -25,7 +25,7 @@ import 
'package:fory/src/codegen/analyze/annotation/require_location_level.dart'
 import 'package:fory/src/codegen/const/location_level.dart';
 import 'package:fory/src/codegen/entity/location_mark.dart';
 import 'package:fory/src/annotation/fory_enum.dart';
-import 'package:fory/src/codegen/exception/annotation_exception.dart';
+import 'package:fory/src/fory_exception.dart';
 
 class EnumAnnotationAnalyzer {
   const EnumAnnotationAnalyzer();
diff --git 
a/dart/packages/fory/lib/src/codegen/analyze/impl/annotation/key_annotation_analyzer.dart
 
b/dart/packages/fory/lib/src/codegen/analyze/impl/annotation/key_annotation_analyzer.dart
index db0b9efa6..5d593bac0 100644
--- 
a/dart/packages/fory/lib/src/codegen/analyze/impl/annotation/key_annotation_analyzer.dart
+++ 
b/dart/packages/fory/lib/src/codegen/analyze/impl/annotation/key_annotation_analyzer.dart
@@ -23,7 +23,7 @@ import 
'package:fory/src/codegen/analyze/analysis_type_identifier.dart';
 import 
'package:fory/src/codegen/analyze/annotation/require_location_level.dart';
 import 'package:fory/src/codegen/const/location_level.dart';
 import 'package:fory/src/codegen/entity/location_mark.dart';
-import 'package:fory/src/codegen/exception/annotation_exception.dart';
+import 'package:fory/src/fory_exception.dart';
 import 'package:fory/src/annotation/fory_key.dart';
 
 class KeyAnnotationAnalyzer {
diff --git 
a/dart/packages/fory/lib/src/codegen/analyze/impl/annotation/uint_annotation_analyzer.dart
 
b/dart/packages/fory/lib/src/codegen/analyze/impl/annotation/uint_annotation_analyzer.dart
index e3d507bfc..b5e607a3f 100644
--- 
a/dart/packages/fory/lib/src/codegen/analyze/impl/annotation/uint_annotation_analyzer.dart
+++ 
b/dart/packages/fory/lib/src/codegen/analyze/impl/annotation/uint_annotation_analyzer.dart
@@ -23,7 +23,7 @@ import 
'package:fory/src/codegen/analyze/analysis_type_identifier.dart';
 import 
'package:fory/src/codegen/analyze/annotation/require_location_level.dart';
 import 'package:fory/src/codegen/const/location_level.dart';
 import 'package:fory/src/codegen/entity/location_mark.dart';
-import 'package:fory/src/codegen/exception/annotation_exception.dart';
+import 'package:fory/src/fory_exception.dart';
 import 'package:fory/src/const/types.dart';
 
 /// Result of uint annotation analysis
diff --git 
a/dart/packages/fory/lib/src/codegen/analyze/impl/constructor/constructor_analyzer.dart
 
b/dart/packages/fory/lib/src/codegen/analyze/impl/constructor/constructor_analyzer.dart
index 45e3a5f15..8c3220758 100644
--- 
a/dart/packages/fory/lib/src/codegen/analyze/impl/constructor/constructor_analyzer.dart
+++ 
b/dart/packages/fory/lib/src/codegen/analyze/impl/constructor/constructor_analyzer.dart
@@ -29,7 +29,7 @@ import 'package:fory/src/codegen/const/location_level.dart';
 import 'package:fory/src/codegen/entity/constructor_param.dart';
 import 'package:fory/src/codegen/entity/constructor_params.dart';
 import 'package:fory/src/codegen/entity/location_mark.dart';
-import 'package:fory/src/codegen/exception/constraint_violation_exception.dart'
+import 'package:fory/src/fory_exception.dart'
     show
         CircularIncapableRisk,
         InformalConstructorParamException,
diff --git 
a/dart/packages/fory/lib/src/codegen/analyze/impl/field/access_info_analyzer.dart
 
b/dart/packages/fory/lib/src/codegen/analyze/impl/field/access_info_analyzer.dart
index b952002c7..e0719454d 100644
--- 
a/dart/packages/fory/lib/src/codegen/analyze/impl/field/access_info_analyzer.dart
+++ 
b/dart/packages/fory/lib/src/codegen/analyze/impl/field/access_info_analyzer.dart
@@ -23,7 +23,7 @@ import 
'package:fory/src/codegen/analyze/annotation/require_location_level.dart'
 import 'package:fory/src/codegen/const/location_level.dart';
 import 'package:fory/src/codegen/entity/constructor_params.dart';
 import 'package:fory/src/codegen/entity/location_mark.dart';
-import 'package:fory/src/codegen/exception/field_exception.dart'
+import 'package:fory/src/fory_exception.dart'
     show FieldAccessErrorType, FieldAccessException;
 import 'package:fory/src/codegen/meta/impl/field_spec_immutable.dart';
 import 'package:fory/src/codegen/meta/impl/fields_spec_generator.dart';
diff --git 
a/dart/packages/fory/lib/src/codegen/analyze/impl/field/field_analyzer_impl.dart
 
b/dart/packages/fory/lib/src/codegen/analyze/impl/field/field_analyzer_impl.dart
index c16f02862..b669fb302 100644
--- 
a/dart/packages/fory/lib/src/codegen/analyze/impl/field/field_analyzer_impl.dart
+++ 
b/dart/packages/fory/lib/src/codegen/analyze/impl/field/field_analyzer_impl.dart
@@ -33,7 +33,7 @@ import 
'package:fory/src/codegen/analyze/interface/field_analyzer.dart';
 import 'package:fory/src/codegen/const/location_level.dart';
 import 'package:fory/src/codegen/entity/either.dart';
 import 'package:fory/src/codegen/entity/location_mark.dart';
-import 
'package:fory/src/codegen/exception/constraint_violation_exception.dart';
+import 'package:fory/src/fory_exception.dart';
 import 'package:fory/src/codegen/meta/impl/field_spec_immutable.dart';
 import 'package:fory/src/codegen/meta/impl/type_spec_generator.dart';
 import 'package:fory/src/codegen/meta/public_accessor_descriptor.dart';
diff --git 
a/dart/packages/fory/lib/src/codegen/analyze/impl/struct/custom_type_analyzer.dart
 
b/dart/packages/fory/lib/src/codegen/analyze/impl/struct/custom_type_analyzer.dart
index 0081a20ca..daf13ede2 100644
--- 
a/dart/packages/fory/lib/src/codegen/analyze/impl/struct/custom_type_analyzer.dart
+++ 
b/dart/packages/fory/lib/src/codegen/analyze/impl/struct/custom_type_analyzer.dart
@@ -21,7 +21,7 @@ import 'package:analyzer/dart/element/element.dart';
 import 'package:fory/src/codegen/analyze/type_analysis_models.dart';
 import 'package:fory/src/codegen/analyze/analyzer.dart';
 import 'package:fory/src/codegen/entity/either.dart';
-import 'package:fory/src/codegen/exception/annotation_exception.dart';
+import 'package:fory/src/fory_exception.dart';
 import 'package:fory/src/const/dart_type.dart';
 
 class CustomTypeAnalyzer {
diff --git 
a/dart/packages/fory/lib/src/codegen/analyze/impl/type/type_system_analyzer.dart
 
b/dart/packages/fory/lib/src/codegen/analyze/impl/type/type_system_analyzer.dart
index 476d1a6ea..938c420a9 100644
--- 
a/dart/packages/fory/lib/src/codegen/analyze/impl/type/type_system_analyzer.dart
+++ 
b/dart/packages/fory/lib/src/codegen/analyze/impl/type/type_system_analyzer.dart
@@ -26,7 +26,7 @@ import 
'package:fory/src/codegen/analyze/annotation/require_location_level.dart'
 import 'package:fory/src/codegen/const/location_level.dart';
 import 'package:fory/src/codegen/entity/either.dart';
 import 'package:fory/src/codegen/entity/location_mark.dart';
-import 
'package:fory/src/codegen/exception/constraint_violation_exception.dart';
+import 'package:fory/src/fory_exception.dart';
 import 'package:fory/src/const/dart_type.dart';
 
 class TypeSystemAnalyzer {
@@ -41,7 +41,7 @@ class TypeSystemAnalyzer {
     Either<ObjectTypeAnalysis, DartTypeEnum> res =
         Analyzer.customTypeAnalyzer.resolveType(element);
     if (res.isRight) {
-      throw UnsupportedTypeException(
+      throw CodegenUnsupportedTypeException(
         locationMark.libPath,
         locationMark.clsName,
         locationMark.fieldName!,
diff --git 
a/dart/packages/fory/lib/src/codegen/exception/annotation_exception.dart 
b/dart/packages/fory/lib/src/codegen/exception/annotation_exception.dart
deleted file mode 100644
index c80909154..000000000
--- a/dart/packages/fory/lib/src/codegen/exception/annotation_exception.dart
+++ /dev/null
@@ -1,180 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-import 'package:meta/meta_meta.dart';
-import 'package:fory/src/codegen/exception/fory_codegen_exception.dart';
-import 'package:fory/src/const/meta_string_const.dart';
-
-abstract class AnnotationException extends ForyCodegenException {
-  AnnotationException(super._where);
-}
-
-class InvalidClassTagException extends ForyCodegenException {
-  final List<String>? _classesWithEmptyTag;
-  final List<String>? _classesWithTooLongTag;
-  final Map<String, List<String>>? _repeatedTags;
-
-  InvalidClassTagException(this._classesWithEmptyTag,
-      this._classesWithTooLongTag, this._repeatedTags,
-      [super._where]) {
-    assert(_classesWithEmptyTag != null ||
-        _repeatedTags != null ||
-        _classesWithTooLongTag != null);
-  }
-
-  @override
-  void giveExceptionMessage(StringBuffer buf) {
-    super.giveExceptionMessage(buf);
-    if (_classesWithEmptyTag != null) {
-      buf.write('Classes with empty tag:');
-      buf.writeAll(_classesWithEmptyTag, ', ');
-      buf.write('\n');
-    }
-
-    if (_classesWithTooLongTag != null) {
-      buf.write('Classes with too long tag (should be less than ');
-      buf.write(MetaStringConst.metaStrMaxLen);
-      buf.write('):');
-      buf.writeAll(_classesWithTooLongTag, ', ');
-      buf.write('\n');
-    }
-
-    if (_repeatedTags != null) {
-      buf.write('Classes with repeated tags:');
-      for (String c in _repeatedTags.keys) {
-        buf.write(c);
-        buf.write(': ');
-        buf.writeAll(_repeatedTags[c]!, ', ');
-        buf.write('\n');
-      }
-    }
-  }
-
-  @override
-  String toString() {
-    final buf = StringBuffer();
-    giveExceptionMessage(buf);
-    return buf.toString();
-  }
-}
-
-class ConflictAnnotationException extends AnnotationException {
-  final String _targetAnnotation;
-  final String _conflictAnnotation;
-
-  ConflictAnnotationException(this._targetAnnotation, this._conflictAnnotation,
-      [super._where]);
-
-  @override
-  void giveExceptionMessage(StringBuffer buf) {
-    super.giveExceptionMessage(buf);
-    buf.write(
-        'The annotation $_targetAnnotation conflicts with 
$_conflictAnnotation.');
-    buf.write('\n');
-  }
-
-  @override
-  String toString() {
-    final buf = StringBuffer();
-    giveExceptionMessage(buf);
-    return buf.toString();
-  }
-}
-
-class DuplicatedAnnotationException extends AnnotationException {
-  final String _annotation;
-  final String _displayName;
-
-  DuplicatedAnnotationException(this._annotation, this._displayName,
-      [super._where]);
-
-  @override
-  void giveExceptionMessage(StringBuffer buf) {
-    super.giveExceptionMessage(buf);
-    buf.write(_displayName);
-    buf.write(' has multiple ');
-    buf.write(_annotation);
-    buf.write(' annotations.');
-    buf.write('\n');
-  }
-
-  @override
-  String toString() {
-    final buf = StringBuffer();
-    giveExceptionMessage(buf);
-    return buf.toString();
-  }
-}
-
-class CodegenUnregisteredTypeException extends AnnotationException {
-  final String _libPath;
-  final String _clsName;
-
-  final String _annotation;
-
-  CodegenUnregisteredTypeException(
-      this._libPath, this._clsName, this._annotation,
-      [super._where]);
-
-  @override
-  void giveExceptionMessage(StringBuffer buf) {
-    super.giveExceptionMessage(buf);
-    buf.write('Unregistered type: ');
-    buf.write(_libPath);
-    buf.write('@');
-    buf.write(_clsName);
-    buf.write('\nit should be registered with the annotation: ');
-    buf.write(_annotation);
-  }
-
-  @override
-  String toString() {
-    final buf = StringBuffer();
-    giveExceptionMessage(buf);
-    return buf.toString();
-  }
-}
-
-class InvalidAnnotationTargetException extends AnnotationException {
-  final String _annotation;
-  final String _theTarget;
-  final List<TargetKind> _supported;
-
-  InvalidAnnotationTargetException(
-      this._annotation, this._theTarget, this._supported,
-      [super._where]);
-
-  @override
-  void giveExceptionMessage(StringBuffer buf) {
-    super.giveExceptionMessage(buf);
-    buf.write('Unsupported target for annotation: ');
-    buf.writeln(_annotation);
-    buf.write('Target: ');
-    buf.writeln(_theTarget);
-    buf.write('Supported targets: ');
-    buf.writeAll(_supported, ', ');
-  }
-
-  @override
-  String toString() {
-    final buf = StringBuffer();
-    giveExceptionMessage(buf);
-    return buf.toString();
-  }
-}
diff --git 
a/dart/packages/fory/lib/src/codegen/exception/constraint_violation_exception.dart
 
b/dart/packages/fory/lib/src/codegen/exception/constraint_violation_exception.dart
deleted file mode 100644
index 9430485a3..000000000
--- 
a/dart/packages/fory/lib/src/codegen/exception/constraint_violation_exception.dart
+++ /dev/null
@@ -1,210 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-import 'package:fory/src/codegen/exception/fory_codegen_exception.dart';
-import 'package:fory/src/codegen/rules/code_rules.dart';
-
-class ClassLevelException extends ForyCodegenException {
-  final String _libPath;
-  final String _className;
-
-  ClassLevelException(this._libPath, this._className, [super._where]);
-
-  @override
-  void giveExceptionMessage(StringBuffer buf) {
-    super.giveExceptionMessage(buf);
-    buf.write('related class: ');
-    buf.write(_libPath);
-    buf.write('@');
-    buf.write(_className);
-    buf.write('\n');
-  }
-
-  @override
-  String toString() {
-    final buf = StringBuffer();
-    giveExceptionMessage(buf);
-    return buf.toString();
-  }
-}
-
-abstract class FieldException extends ForyConstraintViolation {
-  final String _libPath;
-  final String _className;
-  final List<String> _invalidFields;
-
-  FieldException(
-      this._libPath, this._className, this._invalidFields, super._constraint,
-      [super.where]);
-
-  @override
-  void giveExceptionMessage(StringBuffer buf) {
-    super.giveExceptionMessage(buf);
-    buf.write('related class: ');
-    buf.write(_libPath);
-    buf.write('@');
-    buf.write(_className);
-    buf.write('\n');
-    buf.write('invalidFields: ');
-    buf.writeAll(_invalidFields, ', ');
-    buf.write('\n');
-  }
-
-  @override
-  String toString() {
-    StringBuffer buf = StringBuffer();
-    giveExceptionMessage(buf);
-    return buf.toString();
-  }
-}
-
-abstract class ForyConstraintViolation extends ForyCodegenException {
-  final String _constraint;
-
-  ForyConstraintViolation(this._constraint, [super._where]);
-
-  @override
-  void giveExceptionMessage(StringBuffer buf) {
-    super.giveExceptionMessage(buf);
-    buf.write('constraint: ');
-    buf.write(_constraint);
-    buf.write('\n');
-  }
-}
-
-class CircularIncapableRisk extends ForyConstraintViolation {
-  final String libPath;
-  final String className;
-
-  CircularIncapableRisk(
-    this.libPath,
-    this.className,
-  ) : super(
-          CodeRules.circularReferenceIncapableRisk,
-        );
-
-  @override
-  void giveExceptionMessage(StringBuffer buf) {
-    super.giveExceptionMessage(buf);
-    buf.write('related class: ');
-    buf.write(libPath);
-    buf.write('@');
-    buf.write(className);
-    buf.write('\n');
-  }
-
-  @override
-  String toString() {
-    final buf = StringBuffer();
-    giveExceptionMessage(buf);
-    return buf.toString();
-  }
-}
-
-class InformalConstructorParamException extends ClassLevelException {
-  final List<String> _invalidParams;
-
-  // There is no need to add the reason field, because the reason is actually 
just invalidParams
-  InformalConstructorParamException(
-      String libPath, String className, this._invalidParams,
-      [String? where])
-      : super(libPath, className, where);
-
-  @override
-  void giveExceptionMessage(StringBuffer buf) {
-    super.giveExceptionMessage(buf);
-    buf.write(CodeRules.consParamsOnlySupportThisAndSuper);
-    buf.write('invalidParams: ');
-    buf.writeAll(_invalidParams, ', ');
-    buf.write('\n');
-  }
-
-  @override
-  String toString() {
-    final buf = StringBuffer();
-    giveExceptionMessage(buf);
-    return buf.toString();
-  }
-}
-
-class FieldOverridingException extends FieldException {
-  FieldOverridingException(
-      String libPath, String className, List<String> invalidFields,
-      [String? where])
-      : super(libPath, className, invalidFields,
-            CodeRules.unsupportFieldOverriding, where);
-}
-
-class NoUsableConstructorException extends ForyCodegenException {
-  final String libPath;
-  final String className;
-  final String reason;
-
-  NoUsableConstructorException(this.libPath, this.className, this.reason)
-      : super('$libPath@$className');
-}
-
-class UnsupportedTypeException extends ForyCodegenException {
-  final String clsLibPath;
-  final String clsName;
-  final String fieldName;
-
-  final String typeScheme;
-  final String typePath;
-  final String typeName;
-
-  UnsupportedTypeException(
-    this.clsLibPath,
-    this.clsName,
-    this.fieldName,
-    this.typeScheme,
-    this.typePath,
-    this.typeName,
-  ) : super('$clsLibPath@$clsName');
-
-  /// will generate warning and error location
-  @override
-  void giveExceptionMessage(StringBuffer buf) {
-    super.giveExceptionMessage(buf);
-    buf.write('Unsupported type: ');
-    buf.write(typeScheme);
-    buf.write(':');
-    buf.write(typePath);
-    buf.write('@');
-    buf.write(typeName);
-    buf.write('\n');
-  }
-
-  @override
-  String toString() {
-    StringBuffer buf = StringBuffer();
-    giveExceptionMessage(buf);
-    return buf.toString();
-  }
-}
-
-class ConstraintViolationException extends FieldException {
-  ConstraintViolationException(
-    String libPath,
-    String className,
-    String fieldName,
-    String constraint, [
-    String? where,
-  ]) : super(libPath, className, [fieldName], constraint, where);
-}
diff --git a/dart/packages/fory/lib/src/codegen/exception/field_exception.dart 
b/dart/packages/fory/lib/src/codegen/exception/field_exception.dart
deleted file mode 100644
index e6328469b..000000000
--- a/dart/packages/fory/lib/src/codegen/exception/field_exception.dart
+++ /dev/null
@@ -1,49 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-import 
'package:fory/src/codegen/exception/constraint_violation_exception.dart';
-
-enum FieldAccessErrorType {
-  noWayToAssign(
-      "This field needs to be assigned a value because it's includedFromFory, 
but it's not a constructor parameter and can't be assigned via a setter."),
-  noWayToGet(
-      "This field needs to be read because it's includedFromFory, but it's not 
public and it can't be read via a getter."),
-  notIncludedButConsDemand(
-      "This field is included in the constructor, but it's not 
includedFromFory. ");
-
-  final String warning;
-
-  const FieldAccessErrorType(this.warning);
-}
-
-class FieldAccessException extends FieldException {
-  final FieldAccessErrorType errorType;
-
-  FieldAccessException(
-    String libPath,
-    String clsName,
-    List<String> fieldNames,
-    this.errorType,
-  ) : super(
-          libPath,
-          clsName,
-          fieldNames,
-          errorType.warning,
-        );
-}
diff --git 
a/dart/packages/fory/lib/src/codegen/exception/fory_codegen_exception.dart 
b/dart/packages/fory/lib/src/codegen/exception/fory_codegen_exception.dart
deleted file mode 100644
index 57825e61f..000000000
--- a/dart/packages/fory/lib/src/codegen/exception/fory_codegen_exception.dart
+++ /dev/null
@@ -1,39 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-import 'package:fory/src/exception/fory_exception.dart';
-
-abstract class ForyCodegenException extends ForyException {
-  final String? _where;
-  ForyCodegenException([this._where]);
-
-  /// will generate warning and error location
-  @override
-  void giveExceptionMessage(StringBuffer buf) {
-    buf.write('''[FORY]: Analysis error detected!
-You need to make sure your codes don't contain any grammar error itself.
-And review the error messages below, correct the issues, and then REGENERATE 
the code.
-''');
-    if (_where != null && _where.isNotEmpty) {
-      buf.write('where: ');
-      buf.write(_where);
-      buf.write('\n');
-    }
-  }
-}
diff --git 
a/dart/packages/fory/lib/src/codegen/generate/obj_spec_generator.dart 
b/dart/packages/fory/lib/src/codegen/generate/obj_spec_generator.dart
index 8f6a99703..e82d9ca99 100644
--- a/dart/packages/fory/lib/src/codegen/generate/obj_spec_generator.dart
+++ b/dart/packages/fory/lib/src/codegen/generate/obj_spec_generator.dart
@@ -23,7 +23,7 @@ import 'package:source_gen/source_gen.dart';
 import 'package:fory/src/annotation/fory_object.dart';
 import 'package:fory/src/codegen/analyze/analysis_type_identifier.dart';
 import 'package:fory/src/codegen/analyze/analyzer.dart';
-import 'package:fory/src/codegen/exception/annotation_exception.dart';
+import 'package:fory/src/fory_exception.dart';
 import 'package:fory/src/codegen/meta/generated_code_part.dart';
 import 'package:fory/src/annotation/fory_enum.dart';
 import 'package:fory/src/annotation/fory_class.dart';
diff --git 
a/dart/packages/fory/lib/src/exception/deserialization_exception.dart 
b/dart/packages/fory/lib/src/exception/deserialization_exception.dart
deleted file mode 100644
index f8b9ef220..000000000
--- a/dart/packages/fory/lib/src/exception/deserialization_exception.dart
+++ /dev/null
@@ -1,149 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-import 'package:fory/src/exception/fory_exception.dart';
-
-import 'package:fory/src/const/types.dart';
-
-abstract class DeserializationException extends ForyException {
-  final String? _where;
-
-  DeserializationException([this._where]);
-
-  @override
-  void giveExceptionMessage(StringBuffer buf) {
-    if (_where != null) {
-      buf.write('where: ');
-      buf.writeln(_where);
-    }
-  }
-}
-
-class DeserializationConflictException extends DeserializationException {
-  final String _readSetting;
-  final String _nowForySetting;
-
-  DeserializationConflictException(this._readSetting, this._nowForySetting,
-      [super._where]);
-
-  @override
-  void giveExceptionMessage(StringBuffer buf) {
-    super.giveExceptionMessage(buf);
-    buf.write('the fory instance setting: ');
-    buf.writeln(_nowForySetting);
-    buf.write('while the read setting: ');
-    buf.writeln(_readSetting);
-  }
-}
-
-class UnsupportedFeatureException extends DeserializationException {
-  final Object _read;
-  final List<Object> _supported;
-  final String _whatFeature;
-
-  UnsupportedFeatureException(this._read, this._supported, this._whatFeature,
-      [super._where]);
-
-  @override
-  void giveExceptionMessage(StringBuffer buf) {
-    super.giveExceptionMessage(buf);
-    buf.write('unsupported ');
-    buf.write(_whatFeature);
-    buf.write(' for type: ');
-    buf.writeln(_read);
-    buf.write('supported: ');
-    buf.writeAll(_supported, ', ');
-    buf.write('\n');
-  }
-}
-
-class DeserializationRangeException extends ForyException {
-  final int index;
-  final List<Object> candidates;
-
-  DeserializationRangeException(
-    this.index,
-    this.candidates,
-  );
-
-  @override
-  void giveExceptionMessage(StringBuffer buf) {
-    super.giveExceptionMessage(buf);
-    buf.write('the index $index is out of range, the candidates are: ');
-    buf.write('[');
-    buf.writeAll(candidates, ', ');
-    buf.write(']\n');
-    buf.write('This data may have inconsistencies on the other side');
-  }
-}
-
-class InvalidParamException extends DeserializationException {
-  final String _invalidParam;
-  final String _validParams;
-
-  InvalidParamException(this._invalidParam, this._validParams, [super._where]);
-
-  @override
-  void giveExceptionMessage(StringBuffer buf) {
-    super.giveExceptionMessage(buf);
-    buf.write('the invalid param: ');
-    buf.writeln(_invalidParam);
-    buf.write('while the valid params: ');
-    buf.writeln(_validParams);
-  }
-}
-
-class ForyMismatchException extends DeserializationException {
-  final Object readValue;
-  final Object expected;
-  final String specification;
-
-  ForyMismatchException(
-    this.readValue,
-    this.expected,
-    this.specification,
-  );
-
-  @override
-  void giveExceptionMessage(StringBuffer buf) {
-    super.giveExceptionMessage(buf);
-    buf.write('ForyMismatchException: ');
-    buf.write(specification);
-    buf.write('\nread value: ');
-    buf.write(readValue);
-    buf.write(' ,while expected: ');
-    buf.write(expected);
-    buf.write('\n');
-  }
-}
-
-class UnsupportedTypeException extends ForyException {
-  final ObjType _objType;
-
-  UnsupportedTypeException(
-    this._objType,
-  );
-
-  @override
-  void giveExceptionMessage(StringBuffer buf) {
-    super.giveExceptionMessage(buf);
-    buf.write('unsupported type: ');
-    buf.writeln(_objType);
-  }
-}
diff --git a/dart/packages/fory/lib/src/exception/fory_exception.dart 
b/dart/packages/fory/lib/src/exception/fory_exception.dart
deleted file mode 100644
index 71f20e0ac..000000000
--- a/dart/packages/fory/lib/src/exception/fory_exception.dart
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-abstract class ForyException extends Error {
-  ForyException();
-
-  void giveExceptionMessage(StringBuffer buf) {}
-
-  @override
-  String toString() {
-    final buf = StringBuffer();
-    giveExceptionMessage(buf);
-    return buf.toString();
-  }
-}
-
-class InvalidDataException extends ForyException {
-  final String message;
-
-  InvalidDataException(this.message);
-
-  @override
-  void giveExceptionMessage(StringBuffer buf) {
-    buf.write(message);
-  }
-}
diff --git a/dart/packages/fory/lib/src/exception/registration_exception.dart 
b/dart/packages/fory/lib/src/exception/registration_exception.dart
deleted file mode 100644
index 52804f149..000000000
--- a/dart/packages/fory/lib/src/exception/registration_exception.dart
+++ /dev/null
@@ -1,115 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-import 'package:fory/src/exception/fory_exception.dart';
-
-class UnregisteredTagException extends ForyException {
-  final String _tag;
-
-  UnregisteredTagException(this._tag);
-
-  @override
-  void giveExceptionMessage(StringBuffer buf) {
-    super.giveExceptionMessage(buf);
-    buf.write('Unregistered tag: ');
-    buf.writeln(_tag);
-  }
-}
-
-class UnregisteredTypeException extends ForyException {
-  final Object _type;
-
-  UnregisteredTypeException(this._type);
-
-  @override
-  void giveExceptionMessage(StringBuffer buf) {
-    super.giveExceptionMessage(buf);
-    buf.write('Unregistered type: ');
-    buf.writeln(_type);
-  }
-}
-
-class DuplicatedTagRegistrationException extends ForyException {
-  final String _tag;
-  final Type _tagType;
-  final Type _newType;
-
-  DuplicatedTagRegistrationException(this._tag, this._tagType, this._newType);
-
-  @override
-  void giveExceptionMessage(StringBuffer buf) {
-    super.giveExceptionMessage(buf);
-    buf.write('Duplicate registration for tag: ');
-    buf.writeln(_tag);
-    buf.write('\nThis tag is already registered for type: ');
-    buf.writeln(_tagType);
-    buf.write('\nBut you are now trying to register it for type: ');
-    buf.writeln(_newType);
-  }
-}
-
-class DuplicatedTypeRegistrationException extends ForyException {
-  final Type _forType;
-  final Object _newRegistration;
-
-  DuplicatedTypeRegistrationException(this._forType, this._newRegistration);
-
-  @override
-  void giveExceptionMessage(StringBuffer buf) {
-    super.giveExceptionMessage(buf);
-    buf.write('Duplicate registration for type: ');
-    buf.writeln(_forType);
-    buf.write('\nBut you try to register it again with: ');
-    buf.writeln(_newRegistration);
-  }
-}
-
-class DuplicatedUserTypeIdRegistrationException extends ForyException {
-  final int _userTypeId;
-  final Type _registeredType;
-  final Type _newType;
-
-  DuplicatedUserTypeIdRegistrationException(
-      this._userTypeId, this._registeredType, this._newType);
-
-  @override
-  void giveExceptionMessage(StringBuffer buf) {
-    super.giveExceptionMessage(buf);
-    buf.write('Duplicate registration for user type id: ');
-    buf.writeln(_userTypeId);
-    buf.write('\nThis user type id is already registered for type: ');
-    buf.writeln(_registeredType);
-    buf.write('\nBut you are now trying to register it for type: ');
-    buf.writeln(_newType);
-  }
-}
-
-class RegistrationArgumentException extends ForyException {
-  final Object? _arg;
-
-  RegistrationArgumentException(this._arg);
-
-  @override
-  void giveExceptionMessage(StringBuffer buf) {
-    super.giveExceptionMessage(buf);
-    buf.write('Invalid registration argument: ');
-    buf.writeln(_arg);
-    buf.writeln('Expected `String` tag or `int` user type id.');
-  }
-}
diff --git a/dart/packages/fory/lib/src/exception/serialization_exception.dart 
b/dart/packages/fory/lib/src/exception/serialization_exception.dart
deleted file mode 100644
index 41249d939..000000000
--- a/dart/packages/fory/lib/src/exception/serialization_exception.dart
+++ /dev/null
@@ -1,84 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-import 'package:fory/src/exception/fory_exception.dart';
-
-import 'package:fory/src/const/types.dart';
-
-abstract class SerializationException extends ForyException {
-  final String? _where;
-
-  SerializationException([this._where]);
-
-  @override
-  void giveExceptionMessage(StringBuffer buf) {
-    if (_where != null) {
-      buf.write('where: ');
-      buf.writeln(_where);
-    }
-  }
-}
-
-class TypeIncompatibleException extends SerializationException {
-  final ObjType _specified;
-  final String _reason;
-
-  TypeIncompatibleException(this._specified, this._reason, [super._where]);
-
-  @override
-  void giveExceptionMessage(StringBuffer buf) {
-    super.giveExceptionMessage(buf);
-    buf.write('the specified type: ');
-    buf.writeln(_specified);
-    buf.write('while the reason: ');
-    buf.writeln(_reason);
-  }
-}
-
-class SerializationRangeException extends SerializationException {
-  final ObjType _specified;
-  final num _yourValue;
-
-  SerializationRangeException(this._specified, this._yourValue, 
[super._where]);
-
-  @override
-  void giveExceptionMessage(StringBuffer buf) {
-    super.giveExceptionMessage(buf);
-    buf.write('the specified type: ');
-    buf.writeln(_specified);
-    buf.write('while your value: ');
-    buf.writeln(_yourValue);
-  }
-}
-
-class SerializationConflictException extends SerializationException {
-  final String _setting;
-  final String _but;
-
-  SerializationConflictException(this._setting, this._but, [super._where]);
-
-  @override
-  void giveExceptionMessage(StringBuffer buf) {
-    super.giveExceptionMessage(buf);
-    buf.write('the setting: ');
-    buf.writeln(_setting);
-    buf.write('while: ');
-    buf.writeln(_but);
-  }
-}
diff --git a/dart/packages/fory/lib/src/fory_context.dart 
b/dart/packages/fory/lib/src/fory_context.dart
index 20a88336b..8a2468297 100644
--- a/dart/packages/fory/lib/src/fory_context.dart
+++ b/dart/packages/fory/lib/src/fory_context.dart
@@ -19,7 +19,7 @@
 
 import 'dart:collection';
 import 'package:fory/src/config/fory_config.dart';
-import 'package:fory/src/exception/registration_exception.dart'
+import 'package:fory/src/fory_exception.dart'
     show
         DuplicatedTagRegistrationException,
         DuplicatedTypeRegistrationException,
diff --git a/dart/packages/fory/lib/src/fory_exception.dart 
b/dart/packages/fory/lib/src/fory_exception.dart
new file mode 100644
index 000000000..05af609f1
--- /dev/null
+++ b/dart/packages/fory/lib/src/fory_exception.dart
@@ -0,0 +1,728 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import 'package:fory/src/codegen/rules/code_rules.dart';
+import 'package:fory/src/const/types.dart';
+import 'package:meta/meta_meta.dart';
+import 'package:fory/src/const/meta_string_const.dart';
+
+abstract class ForyException extends Error {
+  ForyException();
+
+  void giveExceptionMessage(StringBuffer buf) {}
+
+  @override
+  String toString() {
+    final buf = StringBuffer();
+    giveExceptionMessage(buf);
+    return buf.toString();
+  }
+}
+
+abstract class DeserializationException extends ForyException {
+  final String? _where;
+
+  DeserializationException([this._where]);
+
+  @override
+  void giveExceptionMessage(StringBuffer buf) {
+    if (_where != null) {
+      buf.write('where: ');
+      buf.writeln(_where);
+    }
+  }
+}
+
+class DeserializationConflictException extends DeserializationException {
+  final String _readSetting;
+  final String _nowForySetting;
+
+  DeserializationConflictException(this._readSetting, this._nowForySetting,
+      [super._where]);
+
+  @override
+  void giveExceptionMessage(StringBuffer buf) {
+    super.giveExceptionMessage(buf);
+    buf.write('the fory instance setting: ');
+    buf.writeln(_nowForySetting);
+    buf.write('while the read setting: ');
+    buf.writeln(_readSetting);
+  }
+}
+
+class UnsupportedFeatureException extends DeserializationException {
+  final Object _read;
+  final List<Object> _supported;
+  final String _whatFeature;
+
+  UnsupportedFeatureException(this._read, this._supported, this._whatFeature,
+      [super._where]);
+
+  @override
+  void giveExceptionMessage(StringBuffer buf) {
+    super.giveExceptionMessage(buf);
+    buf.write('unsupported ');
+    buf.write(_whatFeature);
+    buf.write(' for type: ');
+    buf.writeln(_read);
+    buf.write('supported: ');
+    buf.writeAll(_supported, ', ');
+    buf.write('\n');
+  }
+}
+
+class DeserializationRangeException extends ForyException {
+  final int index;
+  final List<Object> candidates;
+
+  DeserializationRangeException(
+    this.index,
+    this.candidates,
+  );
+
+  @override
+  void giveExceptionMessage(StringBuffer buf) {
+    super.giveExceptionMessage(buf);
+    buf.write('the index $index is out of range, the candidates are: ');
+    buf.write('[');
+    buf.writeAll(candidates, ', ');
+    buf.write(']\n');
+    buf.write('This data may have inconsistencies on the other side');
+  }
+}
+
+class InvalidParamException extends DeserializationException {
+  final String _invalidParam;
+  final String _validParams;
+
+  InvalidParamException(this._invalidParam, this._validParams, [super._where]);
+
+  @override
+  void giveExceptionMessage(StringBuffer buf) {
+    super.giveExceptionMessage(buf);
+    buf.write('the invalid param: ');
+    buf.writeln(_invalidParam);
+    buf.write('while the valid params: ');
+    buf.writeln(_validParams);
+  }
+}
+
+class ForyMismatchException extends DeserializationException {
+  final Object readValue;
+  final Object expected;
+  final String specification;
+
+  ForyMismatchException(
+    this.readValue,
+    this.expected,
+    this.specification,
+  );
+
+  @override
+  void giveExceptionMessage(StringBuffer buf) {
+    super.giveExceptionMessage(buf);
+    buf.write('ForyMismatchException: ');
+    buf.write(specification);
+    buf.write('\nread value: ');
+    buf.write(readValue);
+    buf.write(' ,while expected: ');
+    buf.write(expected);
+    buf.write('\n');
+  }
+}
+
+class UnsupportedTypeException extends ForyException {
+  final ObjType _objType;
+
+  UnsupportedTypeException(
+    this._objType,
+  );
+
+  @override
+  void giveExceptionMessage(StringBuffer buf) {
+    super.giveExceptionMessage(buf);
+    buf.write('unsupported type: ');
+    buf.writeln(_objType);
+  }
+}
+
+abstract class SerializationException extends ForyException {
+  final String? _where;
+
+  SerializationException([this._where]);
+
+  @override
+  void giveExceptionMessage(StringBuffer buf) {
+    if (_where != null) {
+      buf.write('where: ');
+      buf.writeln(_where);
+    }
+  }
+}
+
+class TypeIncompatibleException extends SerializationException {
+  final ObjType _specified;
+  final String _reason;
+
+  TypeIncompatibleException(this._specified, this._reason, [super._where]);
+
+  @override
+  void giveExceptionMessage(StringBuffer buf) {
+    super.giveExceptionMessage(buf);
+    buf.write('the specified type: ');
+    buf.writeln(_specified);
+    buf.write('while the reason: ');
+    buf.writeln(_reason);
+  }
+}
+
+class SerializationRangeException extends SerializationException {
+  final ObjType _specified;
+  final num _yourValue;
+
+  SerializationRangeException(this._specified, this._yourValue, 
[super._where]);
+
+  @override
+  void giveExceptionMessage(StringBuffer buf) {
+    super.giveExceptionMessage(buf);
+    buf.write('the specified type: ');
+    buf.writeln(_specified);
+    buf.write('while your value: ');
+    buf.writeln(_yourValue);
+  }
+}
+
+class SerializationConflictException extends SerializationException {
+  final String _setting;
+  final String _but;
+
+  SerializationConflictException(this._setting, this._but, [super._where]);
+
+  @override
+  void giveExceptionMessage(StringBuffer buf) {
+    super.giveExceptionMessage(buf);
+    buf.write('the setting: ');
+    buf.writeln(_setting);
+    buf.write('while: ');
+    buf.writeln(_but);
+  }
+}
+
+class UnregisteredTagException extends ForyException {
+  final String _tag;
+
+  UnregisteredTagException(this._tag);
+
+  @override
+  void giveExceptionMessage(StringBuffer buf) {
+    super.giveExceptionMessage(buf);
+    buf.write('Unregistered tag: ');
+    buf.writeln(_tag);
+  }
+}
+
+class UnregisteredTypeException extends ForyException {
+  final Object _type;
+
+  UnregisteredTypeException(this._type);
+
+  @override
+  void giveExceptionMessage(StringBuffer buf) {
+    super.giveExceptionMessage(buf);
+    buf.write('Unregistered type: ');
+    buf.writeln(_type);
+  }
+}
+
+class DuplicatedTagRegistrationException extends ForyException {
+  final String _tag;
+  final Type _tagType;
+  final Type _newType;
+
+  DuplicatedTagRegistrationException(this._tag, this._tagType, this._newType);
+
+  @override
+  void giveExceptionMessage(StringBuffer buf) {
+    super.giveExceptionMessage(buf);
+    buf.write('Duplicate registration for tag: ');
+    buf.writeln(_tag);
+    buf.write('\nThis tag is already registered for type: ');
+    buf.writeln(_tagType);
+    buf.write('\nBut you are now trying to register it for type: ');
+    buf.writeln(_newType);
+  }
+}
+
+class DuplicatedTypeRegistrationException extends ForyException {
+  final Type _forType;
+  final Object _newRegistration;
+
+  DuplicatedTypeRegistrationException(this._forType, this._newRegistration);
+
+  @override
+  void giveExceptionMessage(StringBuffer buf) {
+    super.giveExceptionMessage(buf);
+    buf.write('Duplicate registration for type: ');
+    buf.writeln(_forType);
+    buf.write('\nBut you try to register it again with: ');
+    buf.writeln(_newRegistration);
+  }
+}
+
+class DuplicatedUserTypeIdRegistrationException extends ForyException {
+  final int _userTypeId;
+  final Type _registeredType;
+  final Type _newType;
+
+  DuplicatedUserTypeIdRegistrationException(
+      this._userTypeId, this._registeredType, this._newType);
+
+  @override
+  void giveExceptionMessage(StringBuffer buf) {
+    super.giveExceptionMessage(buf);
+    buf.write('Duplicate registration for user type id: ');
+    buf.writeln(_userTypeId);
+    buf.write('\nThis user type id is already registered for type: ');
+    buf.writeln(_registeredType);
+    buf.write('\nBut you are now trying to register it for type: ');
+    buf.writeln(_newType);
+  }
+}
+
+class RegistrationArgumentException extends ForyException {
+  final Object? _arg;
+
+  RegistrationArgumentException(this._arg);
+
+  @override
+  void giveExceptionMessage(StringBuffer buf) {
+    super.giveExceptionMessage(buf);
+    buf.write('Invalid registration argument: ');
+    buf.writeln(_arg);
+    buf.writeln('Expected `String` tag or `int` user type id.');
+  }
+}
+
+class InvalidDataException extends ForyException {
+  final String message;
+
+  InvalidDataException(this.message);
+
+  @override
+  void giveExceptionMessage(StringBuffer buf) {
+    buf.write(message);
+  }
+}
+
+abstract class ForyCodegenException extends ForyException {
+  final String? _where;
+  ForyCodegenException([this._where]);
+
+  /// will generate warning and error location
+  @override
+  void giveExceptionMessage(StringBuffer buf) {
+    buf.write('''[FORY]: Analysis error detected!
+You need to make sure your codes don't contain any grammar error itself.
+And review the error messages below, correct the issues, and then REGENERATE 
the code.
+''');
+    if (_where != null && _where.isNotEmpty) {
+      buf.write('where: ');
+      buf.write(_where);
+      buf.write('\n');
+    }
+  }
+}
+
+class ClassLevelException extends ForyCodegenException {
+  final String _libPath;
+  final String _className;
+
+  ClassLevelException(this._libPath, this._className, [super._where]);
+
+  @override
+  void giveExceptionMessage(StringBuffer buf) {
+    super.giveExceptionMessage(buf);
+    buf.write('related class: ');
+    buf.write(_libPath);
+    buf.write('@');
+    buf.write(_className);
+    buf.write('\n');
+  }
+
+  @override
+  String toString() {
+    final buf = StringBuffer();
+    giveExceptionMessage(buf);
+    return buf.toString();
+  }
+}
+
+abstract class FieldException extends ForyConstraintViolation {
+  final String _libPath;
+  final String _className;
+  final List<String> _invalidFields;
+
+  FieldException(
+      this._libPath, this._className, this._invalidFields, super._constraint,
+      [super.where]);
+
+  @override
+  void giveExceptionMessage(StringBuffer buf) {
+    super.giveExceptionMessage(buf);
+    buf.write('related class: ');
+    buf.write(_libPath);
+    buf.write('@');
+    buf.write(_className);
+    buf.write('\n');
+    buf.write('invalidFields: ');
+    buf.writeAll(_invalidFields, ', ');
+    buf.write('\n');
+  }
+
+  @override
+  String toString() {
+    StringBuffer buf = StringBuffer();
+    giveExceptionMessage(buf);
+    return buf.toString();
+  }
+}
+
+abstract class ForyConstraintViolation extends ForyCodegenException {
+  final String _constraint;
+
+  ForyConstraintViolation(this._constraint, [super._where]);
+
+  @override
+  void giveExceptionMessage(StringBuffer buf) {
+    super.giveExceptionMessage(buf);
+    buf.write('constraint: ');
+    buf.write(_constraint);
+    buf.write('\n');
+  }
+}
+
+class CircularIncapableRisk extends ForyConstraintViolation {
+  final String libPath;
+  final String className;
+
+  CircularIncapableRisk(
+    this.libPath,
+    this.className,
+  ) : super(
+          CodeRules.circularReferenceIncapableRisk,
+        );
+
+  @override
+  void giveExceptionMessage(StringBuffer buf) {
+    super.giveExceptionMessage(buf);
+    buf.write('related class: ');
+    buf.write(libPath);
+    buf.write('@');
+    buf.write(className);
+    buf.write('\n');
+  }
+
+  @override
+  String toString() {
+    final buf = StringBuffer();
+    giveExceptionMessage(buf);
+    return buf.toString();
+  }
+}
+
+class InformalConstructorParamException extends ClassLevelException {
+  final List<String> _invalidParams;
+
+  // There is no need to add the reason field, because the reason is actually 
just invalidParams
+  InformalConstructorParamException(
+      String libPath, String className, this._invalidParams,
+      [String? where])
+      : super(libPath, className, where);
+
+  @override
+  void giveExceptionMessage(StringBuffer buf) {
+    super.giveExceptionMessage(buf);
+    buf.write(CodeRules.consParamsOnlySupportThisAndSuper);
+    buf.write('invalidParams: ');
+    buf.writeAll(_invalidParams, ', ');
+    buf.write('\n');
+  }
+
+  @override
+  String toString() {
+    final buf = StringBuffer();
+    giveExceptionMessage(buf);
+    return buf.toString();
+  }
+}
+
+class FieldOverridingException extends FieldException {
+  FieldOverridingException(
+      String libPath, String className, List<String> invalidFields,
+      [String? where])
+      : super(libPath, className, invalidFields,
+            CodeRules.unsupportFieldOverriding, where);
+}
+
+class NoUsableConstructorException extends ForyCodegenException {
+  final String libPath;
+  final String className;
+  final String reason;
+
+  NoUsableConstructorException(this.libPath, this.className, this.reason)
+      : super('$libPath@$className');
+}
+
+class CodegenUnsupportedTypeException extends ForyCodegenException {
+  final String clsLibPath;
+  final String clsName;
+  final String fieldName;
+
+  final String typeScheme;
+  final String typePath;
+  final String typeName;
+
+  CodegenUnsupportedTypeException(
+    this.clsLibPath,
+    this.clsName,
+    this.fieldName,
+    this.typeScheme,
+    this.typePath,
+    this.typeName,
+  ) : super('$clsLibPath@$clsName');
+
+  /// will generate warning and error location
+  @override
+  void giveExceptionMessage(StringBuffer buf) {
+    super.giveExceptionMessage(buf);
+    buf.write('Unsupported type: ');
+    buf.write(typeScheme);
+    buf.write(':');
+    buf.write(typePath);
+    buf.write('@');
+    buf.write(typeName);
+    buf.write('\n');
+  }
+
+  @override
+  String toString() {
+    StringBuffer buf = StringBuffer();
+    giveExceptionMessage(buf);
+    return buf.toString();
+  }
+}
+
+class ConstraintViolationException extends FieldException {
+  ConstraintViolationException(
+    String libPath,
+    String className,
+    String fieldName,
+    String constraint, [
+    String? where,
+  ]) : super(libPath, className, [fieldName], constraint, where);
+}
+
+
+enum FieldAccessErrorType {
+  noWayToAssign(
+      "This field needs to be assigned a value because it's includedFromFory, 
but it's not a constructor parameter and can't be assigned via a setter."),
+  noWayToGet(
+      "This field needs to be read because it's includedFromFory, but it's not 
public and it can't be read via a getter."),
+  notIncludedButConsDemand(
+      "This field is included in the constructor, but it's not 
includedFromFory. ");
+
+  final String warning;
+
+  const FieldAccessErrorType(this.warning);
+}
+
+class FieldAccessException extends FieldException {
+  final FieldAccessErrorType errorType;
+
+  FieldAccessException(
+    String libPath,
+    String clsName,
+    List<String> fieldNames,
+    this.errorType,
+  ) : super(
+          libPath,
+          clsName,
+          fieldNames,
+          errorType.warning,
+        );
+}
+
+abstract class AnnotationException extends ForyCodegenException {
+  AnnotationException(super._where);
+}
+
+class InvalidClassTagException extends ForyCodegenException {
+  final List<String>? _classesWithEmptyTag;
+  final List<String>? _classesWithTooLongTag;
+  final Map<String, List<String>>? _repeatedTags;
+
+  InvalidClassTagException(this._classesWithEmptyTag,
+      this._classesWithTooLongTag, this._repeatedTags,
+      [super._where]) {
+    assert(_classesWithEmptyTag != null ||
+        _repeatedTags != null ||
+        _classesWithTooLongTag != null);
+  }
+
+  @override
+  void giveExceptionMessage(StringBuffer buf) {
+    super.giveExceptionMessage(buf);
+    if (_classesWithEmptyTag != null) {
+      buf.write('Classes with empty tag:');
+      buf.writeAll(_classesWithEmptyTag, ', ');
+      buf.write('\n');
+    }
+
+    if (_classesWithTooLongTag != null) {
+      buf.write('Classes with too long tag (should be less than ');
+      buf.write(MetaStringConst.metaStrMaxLen);
+      buf.write('):');
+      buf.writeAll(_classesWithTooLongTag, ', ');
+      buf.write('\n');
+    }
+
+    if (_repeatedTags != null) {
+      buf.write('Classes with repeated tags:');
+      for (String c in _repeatedTags.keys) {
+        buf.write(c);
+        buf.write(': ');
+        buf.writeAll(_repeatedTags[c]!, ', ');
+        buf.write('\n');
+      }
+    }
+  }
+
+  @override
+  String toString() {
+    final buf = StringBuffer();
+    giveExceptionMessage(buf);
+    return buf.toString();
+  }
+}
+
+class ConflictAnnotationException extends AnnotationException {
+  final String _targetAnnotation;
+  final String _conflictAnnotation;
+
+  ConflictAnnotationException(this._targetAnnotation, this._conflictAnnotation,
+      [super._where]);
+
+  @override
+  void giveExceptionMessage(StringBuffer buf) {
+    super.giveExceptionMessage(buf);
+    buf.write(
+        'The annotation $_targetAnnotation conflicts with 
$_conflictAnnotation.');
+    buf.write('\n');
+  }
+
+  @override
+  String toString() {
+    final buf = StringBuffer();
+    giveExceptionMessage(buf);
+    return buf.toString();
+  }
+}
+
+class DuplicatedAnnotationException extends AnnotationException {
+  final String _annotation;
+  final String _displayName;
+
+  DuplicatedAnnotationException(this._annotation, this._displayName,
+      [super._where]);
+
+  @override
+  void giveExceptionMessage(StringBuffer buf) {
+    super.giveExceptionMessage(buf);
+    buf.write(_displayName);
+    buf.write(' has multiple ');
+    buf.write(_annotation);
+    buf.write(' annotations.');
+    buf.write('\n');
+  }
+
+  @override
+  String toString() {
+    final buf = StringBuffer();
+    giveExceptionMessage(buf);
+    return buf.toString();
+  }
+}
+
+class CodegenUnregisteredTypeException extends AnnotationException {
+  final String _libPath;
+  final String _clsName;
+
+  final String _annotation;
+
+  CodegenUnregisteredTypeException(
+      this._libPath, this._clsName, this._annotation,
+      [super._where]);
+
+  @override
+  void giveExceptionMessage(StringBuffer buf) {
+    super.giveExceptionMessage(buf);
+    buf.write('Unregistered type: ');
+    buf.write(_libPath);
+    buf.write('@');
+    buf.write(_clsName);
+    buf.write('\nit should be registered with the annotation: ');
+    buf.write(_annotation);
+  }
+
+  @override
+  String toString() {
+    final buf = StringBuffer();
+    giveExceptionMessage(buf);
+    return buf.toString();
+  }
+}
+
+class InvalidAnnotationTargetException extends AnnotationException {
+  final String _annotation;
+  final String _theTarget;
+  final List<TargetKind> _supported;
+
+  InvalidAnnotationTargetException(
+      this._annotation, this._theTarget, this._supported,
+      [super._where]);
+
+  @override
+  void giveExceptionMessage(StringBuffer buf) {
+    super.giveExceptionMessage(buf);
+    buf.write('Unsupported target for annotation: ');
+    buf.writeln(_annotation);
+    buf.write('Target: ');
+    buf.writeln(_theTarget);
+    buf.write('Supported targets: ');
+    buf.writeAll(_supported, ', ');
+  }
+
+  @override
+  String toString() {
+    final buf = StringBuffer();
+    giveExceptionMessage(buf);
+    return buf.toString();
+  }
+}
+
diff --git a/dart/packages/fory/lib/src/resolver/impl/type_resolver_impl.dart 
b/dart/packages/fory/lib/src/resolver/impl/type_resolver_impl.dart
index cb2ff6a52..ccc75b686 100644
--- a/dart/packages/fory/lib/src/resolver/impl/type_resolver_impl.dart
+++ b/dart/packages/fory/lib/src/resolver/impl/type_resolver_impl.dart
@@ -27,7 +27,7 @@ import 
'package:fory/src/codegen/entity/struct_hash_pair.dart';
 import 'package:fory/src/collection/long_long_key.dart';
 import 'package:fory/src/const/types.dart';
 import 'package:fory/src/dev_annotation/optimize.dart';
-import 'package:fory/src/exception/registration_exception.dart'
+import 'package:fory/src/fory_exception.dart'
     show
         RegistrationArgumentException,
         UnregisteredTagException,
@@ -56,7 +56,7 @@ import 'package:fory/src/serialization_context.dart';
 import 'package:fory/src/util/murmur3hash.dart';
 import 'package:fory/src/util/string_util.dart';
 
-import '../../exception/deserialization_exception.dart'
+import '../../fory_exception.dart'
     show UnsupportedTypeException;
 
 final class TypeResolverImpl extends TypeResolver {
diff --git a/dart/packages/fory/lib/src/serializer/array_serializer.dart 
b/dart/packages/fory/lib/src/serializer/array_serializer.dart
index 6ad67ded5..81e1a4552 100644
--- a/dart/packages/fory/lib/src/serializer/array_serializer.dart
+++ b/dart/packages/fory/lib/src/serializer/array_serializer.dart
@@ -20,7 +20,7 @@
 import 'dart:typed_data';
 import 'package:fory/src/const/types.dart';
 import 'package:fory/src/deserialization_context.dart';
-import 'package:fory/src/exception/fory_exception.dart';
+import 'package:fory/src/fory_exception.dart';
 import 'package:fory/src/memory/byte_reader.dart';
 import 'package:fory/src/memory/byte_writer.dart';
 import 'package:fory/src/serialization_context.dart';
diff --git a/dart/packages/fory/lib/src/serializer/class_serializer.dart 
b/dart/packages/fory/lib/src/serializer/class_serializer.dart
index 750dbe215..4d35eadc6 100644
--- a/dart/packages/fory/lib/src/serializer/class_serializer.dart
+++ b/dart/packages/fory/lib/src/serializer/class_serializer.dart
@@ -21,7 +21,7 @@ import 
'package:fory/src/codegen/entity/struct_hash_pair.dart';
 import 'package:fory/src/config/fory_config.dart';
 import 'package:fory/src/const/types.dart';
 import 'package:fory/src/deserialization_context.dart';
-import 'package:fory/src/exception/deserialization_exception.dart';
+import 'package:fory/src/fory_exception.dart';
 import 'package:fory/src/memory/byte_reader.dart';
 import 'package:fory/src/memory/byte_writer.dart';
 import 'package:fory/src/meta/spec_wraps/type_spec_wrap.dart';
diff --git 
a/dart/packages/fory/lib/src/serializer/collection/list/list_serializer.dart 
b/dart/packages/fory/lib/src/serializer/collection/list/list_serializer.dart
index 67bb80f0d..0b8bb6d3f 100644
--- a/dart/packages/fory/lib/src/serializer/collection/list/list_serializer.dart
+++ b/dart/packages/fory/lib/src/serializer/collection/list/list_serializer.dart
@@ -20,7 +20,7 @@
 import 'package:fory/src/const/ref_flag.dart';
 import 'package:fory/src/const/types.dart';
 import 'package:fory/src/deserialization_context.dart';
-import 'package:fory/src/exception/fory_exception.dart';
+import 'package:fory/src/fory_exception.dart';
 import 'package:fory/src/memory/byte_reader.dart';
 import 'package:fory/src/meta/spec_wraps/type_spec_wrap.dart';
 import 'package:fory/src/serializer/collection/iterable_serializer.dart';
diff --git 
a/dart/packages/fory/lib/src/serializer/collection/map/map_serializer.dart 
b/dart/packages/fory/lib/src/serializer/collection/map/map_serializer.dart
index 8176b8716..ee52af598 100644
--- a/dart/packages/fory/lib/src/serializer/collection/map/map_serializer.dart
+++ b/dart/packages/fory/lib/src/serializer/collection/map/map_serializer.dart
@@ -18,7 +18,7 @@
  */
 
 import 'package:fory/src/deserialization_context.dart';
-import 'package:fory/src/exception/fory_exception.dart';
+import 'package:fory/src/fory_exception.dart';
 import 'package:fory/src/meta/spec_wraps/type_spec_wrap.dart';
 import 'package:fory/src/const/types.dart';
 import 'package:fory/src/memory/byte_reader.dart';
diff --git 
a/dart/packages/fory/lib/src/serializer/collection/set/set_serializer.dart 
b/dart/packages/fory/lib/src/serializer/collection/set/set_serializer.dart
index b2612f29d..c6b4fbbad 100644
--- a/dart/packages/fory/lib/src/serializer/collection/set/set_serializer.dart
+++ b/dart/packages/fory/lib/src/serializer/collection/set/set_serializer.dart
@@ -28,7 +28,7 @@ library;
 import 'package:fory/src/const/ref_flag.dart';
 import 'package:fory/src/const/types.dart';
 import 'package:fory/src/deserialization_context.dart';
-import 'package:fory/src/exception/fory_exception.dart';
+import 'package:fory/src/fory_exception.dart';
 import 'package:fory/src/memory/byte_reader.dart';
 import 'package:fory/src/meta/spec_wraps/type_spec_wrap.dart';
 import 'package:fory/src/serializer/collection/iterable_serializer.dart';
diff --git a/dart/packages/fory/lib/src/serializer/enum_serializer.dart 
b/dart/packages/fory/lib/src/serializer/enum_serializer.dart
index a62391aec..57d590338 100644
--- a/dart/packages/fory/lib/src/serializer/enum_serializer.dart
+++ b/dart/packages/fory/lib/src/serializer/enum_serializer.dart
@@ -20,7 +20,7 @@
 import 'package:fory/src/config/fory_config.dart';
 import 'package:fory/src/const/types.dart';
 import 'package:fory/src/deserialization_context.dart';
-import 'package:fory/src/exception/deserialization_exception.dart';
+import 'package:fory/src/fory_exception.dart';
 import 'package:fory/src/memory/byte_reader.dart';
 import 'package:fory/src/memory/byte_writer.dart';
 import 'package:fory/src/meta/specs/enum_spec.dart';
diff --git a/dart/packages/fory/lib/src/serializer/string_serializer.dart 
b/dart/packages/fory/lib/src/serializer/string_serializer.dart
index 2fa879b4e..f5bd8a248 100644
--- a/dart/packages/fory/lib/src/serializer/string_serializer.dart
+++ b/dart/packages/fory/lib/src/serializer/string_serializer.dart
@@ -22,7 +22,7 @@ import 'dart:typed_data';
 import 'package:fory/src/config/fory_config.dart';
 import 'package:fory/src/const/types.dart';
 import 'package:fory/src/deserialization_context.dart';
-import 'package:fory/src/exception/deserialization_exception.dart';
+import 'package:fory/src/fory_exception.dart';
 import 'package:fory/src/memory/byte_reader.dart';
 import 'package:fory/src/memory/byte_writer.dart';
 import 'package:fory/src/serializer/serializer.dart';


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to