From 5c5620d97fb38a801169599201fa7257ef561d4b Mon Sep 17 00:00:00 2001 From: reeshika-h Date: Tue, 25 Nov 2025 15:21:56 +0530 Subject: [PATCH 1/2] Update pubspec.yaml for SDK and dependency versions; refactor error handling in contentstack_utils.dart, GQL.dart, and Automate.dart to use centralized error messages; enhance Metadata and Option classes for null safety; adjust tests for updated functionality. --- lib/contentstack_utils.dart | 13 +++++++------ lib/src/GQL.dart | 3 ++- lib/src/constants/ErrorMessages.dart | 14 ++++++++++++++ lib/src/helper/Automate.dart | 14 ++++++++------ lib/src/helper/Metadata.dart | 28 ++++++++++++++-------------- lib/src/model/Option.dart | 12 +++++++----- pubspec.yaml | 14 +++++++------- test/utils_test.dart | 3 ++- 8 files changed, 61 insertions(+), 40 deletions(-) create mode 100644 lib/src/constants/ErrorMessages.dart diff --git a/lib/contentstack_utils.dart b/lib/contentstack_utils.dart index a4ca304..e5186b8 100644 --- a/lib/contentstack_utils.dart +++ b/lib/contentstack_utils.dart @@ -1,5 +1,6 @@ library contentstack_utils; +import 'package:contentstack_utils/src/constants/ErrorMessages.dart'; import 'package:contentstack_utils/src/helper/Automate.dart'; import 'package:contentstack_utils/src/model/Option.dart'; export 'src/embedded/StyleType.dart'; @@ -8,14 +9,14 @@ export 'src/helper/Metadata.dart'; class Utils { static void render(jsonObject, List rteKeys, Option option) { if (!Automate.isValidJson(jsonObject)) { - throw FormatException('Invalid file, Can\'t process the json file'); + throw FormatException(ErrorMessages.invalidJsonFile); } if (jsonObject is List) { for (var entry in jsonObject) { render(entry, rteKeys, option); } - } else if (jsonObject is Map) { + } else if (jsonObject is Map) { if (jsonObject.containsKey('_embedded_items')) { if (rteKeys.isNotEmpty) { for (var path in rteKeys) { @@ -24,8 +25,8 @@ class Utils { }); } } else { - Map embeddedKeys = jsonObject['_embedded_items']; - rteKeys = embeddedKeys.keys.toList(); + Map embeddedKeys = jsonObject['_embedded_items'] as Map; + rteKeys = embeddedKeys.keys.toList().cast(); embeddedKeys.keys.forEach((keyPath) { Automate.find_embed_keys(jsonObject, keyPath, (rteContent) { return renderContent(rteContent, jsonObject, option); @@ -34,7 +35,7 @@ class Utils { } } } else { - FormatException('Invalid file for embedded objects'); + throw FormatException(ErrorMessages.invalidEmbeddedObjectsInput); } } @@ -69,7 +70,7 @@ class Utils { static void jsonToHTML(items, List key_path, Option option) { if (!Automate.isValidJson(items)) { - throw FormatException('Invalid file, Can\'t process the json file'); + throw FormatException(ErrorMessages.invalidJsonFile); } if (items is List) { diff --git a/lib/src/GQL.dart b/lib/src/GQL.dart index 47fa505..958b104 100644 --- a/lib/src/GQL.dart +++ b/lib/src/GQL.dart @@ -1,10 +1,11 @@ +import 'package:contentstack_utils/src/constants/ErrorMessages.dart'; import 'package:contentstack_utils/src/helper/Automate.dart'; import 'package:contentstack_utils/src/model/Option.dart'; class GQL { static void jsonToHTML(items, List key_path, Option option) { if (!Automate.isValidJson(items)) { - throw FormatException('Invalid file, Can\'t process the json file'); + throw FormatException(ErrorMessages.invalidJsonFile); } if (items is List) { diff --git a/lib/src/constants/ErrorMessages.dart b/lib/src/constants/ErrorMessages.dart new file mode 100644 index 0000000..2d8c7ba --- /dev/null +++ b/lib/src/constants/ErrorMessages.dart @@ -0,0 +1,14 @@ +/// Error message constants for Contentstack Utils +/// +/// This file contains all error messages used throughout the library +/// to ensure consistency and ease of maintenance. +class ErrorMessages { + // JSON validation errors + static const String invalidJsonFile = + 'Invalid JSON file. Provide a valid JSON file and try again.'; + + // Embedded objects errors + static const String invalidEmbeddedObjectsInput = + 'Invalid input for embedded objects. Expected a Map or List.'; +} + diff --git a/lib/src/helper/Automate.dart b/lib/src/helper/Automate.dart index fcf6011..09e4a42 100644 --- a/lib/src/helper/Automate.dart +++ b/lib/src/helper/Automate.dart @@ -21,13 +21,15 @@ class Automate { } } - static Map findEmbeddedItems(Map jsonObject, Metadata metadata) { + static Map? findEmbeddedItems(Map jsonObject, Metadata metadata) { var keys = jsonObject.keys; for (var item in keys) { List jsonArray = jsonObject[item]; var filteredContent = jsonArray - .firstWhere((element) => element['uid'] == metadata.getItemUid); - return filteredContent; + .firstWhere((element) => element['uid'] == metadata.getItemUid, orElse: () => null); + if (filteredContent != null) { + return filteredContent; + } } return null; } @@ -169,7 +171,7 @@ class Automate { } } - static Object findEmbeddedEntry(List jsonList, Metadata metadata) { + static Object? findEmbeddedEntry(List jsonList, Metadata metadata) { for (var obj in jsonList) { if (obj is Map) { if (obj['uid'] == metadata.getItemUid) { @@ -189,8 +191,8 @@ class Automate { return null; } - static String getStringOption(Option option, Metadata meta, Map content) { - var stringOption = option.renderOption(content, meta); + static String getStringOption(Option option, Metadata meta, Object content) { + var stringOption = option.renderOption(content as Map, meta); return stringOption; } diff --git a/lib/src/helper/Metadata.dart b/lib/src/helper/Metadata.dart index aa56fab..5012d31 100644 --- a/lib/src/helper/Metadata.dart +++ b/lib/src/helper/Metadata.dart @@ -3,13 +3,13 @@ import 'dart:collection'; import 'package:html/dom.dart'; class Metadata { - String text; - String itemType; - String itemUid; - String contentTypeUid; - String styleType; - String outerHTML; - LinkedHashMap attributes; + String? text; + String? itemType; + String? itemUid; + String? contentTypeUid; + String? styleType; + String? outerHTML; + LinkedHashMap? attributes; Metadata( {this.text, @@ -20,19 +20,19 @@ class Metadata { this.outerHTML, this.attributes}); - String get getText => text; + String? get getText => text; - String get getItemType => itemType; + String? get getItemType => itemType; - String get getItemUid => itemUid; + String? get getItemUid => itemUid; - String get getContentTypeUid => contentTypeUid; + String? get getContentTypeUid => contentTypeUid; - String get getStyleType => styleType; + String? get getStyleType => styleType; - String get getOuterHTML => outerHTML; + String? get getOuterHTML => outerHTML; - LinkedHashMap get getAttributes => attributes; + LinkedHashMap? get getAttributes => attributes; Metadata.element(Element element) { text = element.text; diff --git a/lib/src/model/Option.dart b/lib/src/model/Option.dart index 6cdd5ee..1eb127e 100644 --- a/lib/src/model/Option.dart +++ b/lib/src/model/Option.dart @@ -21,8 +21,9 @@ class Option { static String _findInlineLink(Metadata metadata, Map obj) { var _title = ''; - if (metadata.getText.isNotEmpty) { - _title = metadata.getText; + var metadataText = metadata.getText; + if (metadataText != null && metadataText.isNotEmpty) { + _title = metadataText; } else if (obj.containsKey('title')) { _title = obj['title']; } else if (obj.containsKey('uid')) { @@ -42,8 +43,9 @@ class Option { static String _findDisplayAtrr(Metadata metadata, Map obj) { var _title = ''; - if (metadata.attributes.isNotEmpty) { - _title = metadata.attributes.toString(); + var metadataAttrs = metadata.attributes; + if (metadataAttrs != null && metadataAttrs.isNotEmpty) { + _title = metadataAttrs.toString(); } else if (obj.containsKey('title')) { _title = obj['title']; } else if (obj.containsKey('filename')) { @@ -60,7 +62,7 @@ class Option { } String renderOption(Map obj, Metadata metadata) { - var style = metadata.styleType; + var style = metadata.styleType ?? ''; switch (style) { case 'block': var titlOrUid = _findTitleOrUid(obj); diff --git a/pubspec.yaml b/pubspec.yaml index 33b9563..38e3af3 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -4,17 +4,17 @@ version: 1.1.0 homepage: https://www.contentstack.com environment: - sdk: ">=2.8.0 <3.0.0" + sdk: ">=2.12.0 <4.0.0" dependencies: #dartdoc: ^1.0.2 - lint: ^1.3.0 - path: ^1.7.0 - html: ^0.14.0+4 - logger: ^1.0.0 + lint: ^2.0.0 + path: ^1.8.0 + html: ^0.15.6 + logger: ^2.0.0 dev_dependencies: - pedantic: ^1.11.1 - test: ^1.17.5 + lints: ^2.0.0 + test: ^1.24.0 # test_coverage: ^0.4.1 \ No newline at end of file diff --git a/test/utils_test.dart b/test/utils_test.dart index a616783..2ea2f88 100644 --- a/test/utils_test.dart +++ b/test/utils_test.dart @@ -48,7 +48,8 @@ void main() { final _item = _entryArray['entries'][0]; final option = Option.entry(_item); final result = Utils.renderContent(__stringHtmlEntry, _item, option); - logger.i(result); + //logger.i(result); + expect(result, isNotNull); }); test('utils.render pass invalid json to cover exception', () { From d41c36bcb6fb4ce9dc7dba61139dfb1322867236 Mon Sep 17 00:00:00 2001 From: reeshika-h Date: Thu, 8 Jan 2026 13:17:59 +0530 Subject: [PATCH 2/2] version bump --- CHANGELOG.md | 5 +++++ LICENSE | 2 +- pubspec.yaml | 2 +- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7923521..9f8c574 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## v1.1.1 + +- Improved error messages +___________________ + ## v1.1.0 Implemented SRTE functions: :tada: diff --git a/LICENSE b/LICENSE index 3855421..b652690 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ # MIT License -Copyright (c) 2012 - 2021 Contentstack +Copyright (c) 2012 - 2026 Contentstack Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/pubspec.yaml b/pubspec.yaml index 38e3af3..cca5f5f 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: contentstack_utils description: Utils package for Contentstack-dart -version: 1.1.0 +version: 1.1.1 homepage: https://www.contentstack.com environment: