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 a52ed2b562..780c190987 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 2fdca3c808..fbb5fbc56c 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 44be37a18e..5dac97bc6d 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 db0b9efa60..5d593bac07 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 e3d507bfc9..b5e607a3f3 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 45e3a5f15a..8c32207587 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 b952002c78..e0719454dd 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 c16f02862a..b669fb3022 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 0081a20ca6..daf13ede27 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 476d1a6eab..938c420a99 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 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 c809091542..0000000000 --- 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? _classesWithEmptyTag; - final List? _classesWithTooLongTag; - final Map>? _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 _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 9430485a3f..0000000000 --- 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 _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 _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 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 e6328469b5..0000000000 --- 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 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 57825e61f6..0000000000 --- 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 8f6a997030..e82d9ca99a 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 f8b9ef2202..0000000000 --- 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 _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 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 71f20e0ac8..0000000000 --- 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 52804f149c..0000000000 --- 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 41249d9392..0000000000 --- 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 20a88336be..8a24682970 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 0000000000..05af609f15 --- /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 _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 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 _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 _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 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 fieldNames, + this.errorType, + ) : super( + libPath, + clsName, + fieldNames, + errorType.warning, + ); +} + +abstract class AnnotationException extends ForyCodegenException { + AnnotationException(super._where); +} + +class InvalidClassTagException extends ForyCodegenException { + final List? _classesWithEmptyTag; + final List? _classesWithTooLongTag; + final Map>? _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 _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 cb2ff6a526..ccc75b6861 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 6ad67ded58..81e1a45529 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 750dbe2158..4d35eadc6e 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 67bb80f0d7..0b8bb6d3fe 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 8176b87163..ee52af5987 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 b2612f29d7..c6b4fbbada 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 a62391aec6..57d5903387 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 2fa879b4eb..f5bd8a2484 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';