Skip to content

Commit 2be26d3

Browse files
Merge pull request #31 from contentstack/fix/DX-3743-improve-error-msgs
Refactor console messages in ModelGenerator
2 parents e1fa78a + 3c2cc6b commit 2be26d3

File tree

5 files changed

+96
-41
lines changed

5 files changed

+96
-41
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
### Version: 0.5.1
2+
#### Date: Jan-12-2026
3+
4+
- Improved Error messages
5+
16
### Version: 0.5.0
27
#### Date: Oct-05-2025
38

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
namespace contentstack.model.generator
2+
{
3+
/// <summary>
4+
/// Centralized console messages for the Contentstack Model Generator
5+
/// </summary>
6+
public static class Messages
7+
{
8+
// Error Messages
9+
public const string UnexpectedError = "Unexpected error occurred. See logs for details.";
10+
public const string OperationFailed = "Operation failed. See error details in logs.";
11+
public const string ApiCommunicationError = "API communication error. Check your network or configuration and try again.";
12+
public const string AuthenticationFailed = "Authentication failed. Verify your API key and auth token and try again.";
13+
14+
// Path Messages
15+
public static string NoPathSpecified(string path) => $"No path specified. Generating files in the current working directory: {path}.";
16+
public static string OutputPathSpecified(string path) => $"Output path specified. Generating files at: {path}.";
17+
public static string OutputPathNotFound(string path) => $"Output path not found. Creating: {path}.";
18+
public static string OpeningOutputDirectory(string directory) => $"Opening output directory: {directory}.";
19+
20+
// Content Types Messages
21+
public const string FetchingStackDetails = "Fetching stack details for the provided API key.";
22+
public static string FetchingContentTypes(string stackName) => $"Fetching content types from {stackName} stack.";
23+
public static string FoundContentTypes(int count) => $"Found {count} content types.";
24+
public static string FetchedContentTypes(int count) => $"Fetched {count} content types.";
25+
public static string TotalContentTypesFetched(int count) => $"Total content types fetched: {count}.";
26+
27+
// Global Fields Messages
28+
public static string FetchingGlobalFields(string stackName) => $"Fetching global fields from stack: {stackName}.";
29+
public static string FoundGlobalFields(int count) => $"Found {count} global fields.";
30+
public static string FetchedGlobalFields(int count) => $"Fetched {count} global fields.";
31+
public static string TotalGlobalFieldsFetched(int count) => $"Total global fields fetched: {count}.";
32+
33+
// File Generation Messages
34+
public const string GeneratingFiles = "Generating files from content types.";
35+
public const string FilesCreatedSuccessfully = "Files created successfully.";
36+
public static string SkippingFile(string fileName) => $"Skipping {fileName} file.";
37+
public static string AddingFile(string fileName, string directory) => $"Adding {fileName} file to {directory}";
38+
39+
// Field Messages
40+
public static string FieldDataType(string dataType) => $"Field data type: {dataType}.";
41+
42+
// Modular Blocks and Groups Messages
43+
public static string ExtractingModularBlocksInContentType(string contentTypeName) => $"Extracting modular blocks in {contentTypeName} Content Type";
44+
public static string ExtractingGroupsInContentType(string contentTypeName) => $"Extracting groups in {contentTypeName} Content Type";
45+
public static string ExtractingModularBlocksInGroup(string groupName) => $"Extracting modular blocks in {groupName} group.";
46+
public static string ExtractingGroupsInGroup(string groupName) => $"Extracting groups in {groupName} group.";
47+
}
48+
}
49+

contentstack.model.generator/ModelGenerator.cs

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public class ModelGenerator
6868
[Option(CommandOptionType.NoValue, ShortName = "N", LongName = "is-nullable", Description = "The features that protect against throwing a System.NullReferenceException can be disruptive when turned on.")]
6969
public bool IsNullable { get; }
7070

71-
[VersionOption("0.5.0")]
71+
[VersionOption("0.5.1")]
7272
public bool Version { get; }
7373

7474
private readonly string _templateStart = @"using System;
@@ -150,73 +150,73 @@ public async Task<int> OnExecute(CommandLineApplication app, IConsole console)
150150

151151
try
152152
{
153-
Console.WriteLine($"Fetching Stack details for API Key {ApiKey}");
153+
Console.WriteLine(Messages.FetchingStackDetails);
154154
stack = await client.GetStack();
155155
}
156156
catch (Exception e)
157157
{
158158
Console.WriteLine(e);
159159
Console.ForegroundColor = ConsoleColor.Red;
160-
Console.Error.WriteLine("There was an error communicating with the Contentstack API: " + e.Message);
161-
Console.Error.WriteLine("Please verify that your api key and authtoken are correct");
160+
Console.Error.WriteLine(Messages.ApiCommunicationError);
161+
Console.Error.WriteLine(Messages.AuthenticationFailed);
162162
return Program.ERROR;
163163
}
164164

165165
var path = "";
166166
if (string.IsNullOrEmpty(Path))
167167
{
168168
path = Directory.GetCurrentDirectory();
169-
Console.WriteLine($"No path specified, creating files in current working directory {path}");
169+
Console.WriteLine(Messages.NoPathSpecified(path));
170170
}
171171
else
172172
{
173-
Console.WriteLine($"Path specified. Files will be created at {Path}");
173+
Console.WriteLine(Messages.OutputPathSpecified(Path));
174174
path = Path;
175175
}
176176
path = $"{path}/Models";
177177
DirectoryInfo dir = CreateDirectory(path);
178178

179179
try
180180
{
181-
Console.WriteLine($"Fetching Content Types from {stack.Name}");
181+
Console.WriteLine(Messages.FetchingContentTypes(stack.Name));
182182
var totalCount = await getContentTypes(client, 0);
183183
var skip = 100;
184-
Console.WriteLine($"Found {totalCount} Content Types .");
184+
Console.WriteLine(Messages.FoundContentTypes(totalCount));
185185

186186
while (totalCount > skip)
187187
{
188-
Console.WriteLine($"{skip} Content Types Fetched.");
188+
Console.WriteLine(Messages.FetchedContentTypes(skip));
189189
totalCount = await getContentTypes(client, skip);
190190
skip += 100;
191191
}
192-
Console.WriteLine($"Total {totalCount} Content Types fetched.");
192+
Console.WriteLine(Messages.TotalContentTypesFetched(totalCount));
193193

194194
CreateEmbeddedObjectClass(Namespace, dir);
195195

196-
Console.WriteLine($"Fetching Global Fields from {stack.Name}");
196+
Console.WriteLine(Messages.FetchingGlobalFields(stack.Name));
197197
totalCount = await getGlobalFields(client, 0);
198198
skip = 100;
199-
Console.WriteLine($"Found {totalCount} Global Fields.");
199+
Console.WriteLine(Messages.FoundGlobalFields(totalCount));
200200

201201
while (totalCount > skip)
202202
{
203-
Console.WriteLine($"{skip} Global Fields Fetched.");
203+
Console.WriteLine(Messages.FetchedGlobalFields(skip));
204204
totalCount = await getGlobalFields(client, skip);
205205
skip += 100;
206206
}
207-
Console.WriteLine($"Total {totalCount} Global Fields fetched.");
207+
Console.WriteLine(Messages.TotalGlobalFieldsFetched(totalCount));
208208
}
209209
catch (Exception e)
210210
{
211211
Console.ForegroundColor = ConsoleColor.Red;
212-
Console.Error.WriteLine("There was an error communicating with the Contentstack API: " + e.Message);
213-
Console.Error.WriteLine("Please verify that your api key and authtoken are correct");
212+
Console.Error.WriteLine(Messages.ApiCommunicationError);
213+
Console.Error.WriteLine(Messages.AuthenticationFailed);
214214
return Program.ERROR;
215215
}
216216
Console.ResetColor();
217217

218218
Console.ForegroundColor = ConsoleColor.Green;
219-
Console.WriteLine($"Generating files from content type");
219+
Console.WriteLine(Messages.GeneratingFiles);
220220
Console.ResetColor();
221221

222222
Console.ForegroundColor = ConsoleColor.DarkYellow;
@@ -231,9 +231,9 @@ public async Task<int> OnExecute(CommandLineApplication app, IConsole console)
231231
}
232232

233233
Console.ForegroundColor = ConsoleColor.Green;
234-
Console.WriteLine($"Files successfully created!");
234+
Console.WriteLine(Messages.FilesCreatedSuccessfully);
235235
Console.ResetColor();
236-
Console.WriteLine($"Opening {dir.FullName}");
236+
Console.WriteLine(Messages.OpeningOutputDirectory(dir.FullName));
237237
OpenFolderatPath(dir.FullName);
238238

239239
// Logout from OAuth if OAuth was used
@@ -306,7 +306,7 @@ private string GetDatatype(Field field, string contentTypeName)
306306
case "group":
307307
return GetDataTypeForGroup(field, contentTypeName);
308308
default:
309-
Console.WriteLine(field.DataType);
309+
Console.WriteLine(Messages.FieldDataType(field.DataType));
310310
break;
311311
}
312312
return "object";
@@ -625,7 +625,7 @@ private void CreateGlobalFieldModular(string contentTypeName, string nameSpace,
625625
private void CreateModularFile(string contentTypeName, string nameSpace, Contenttype contentType, DirectoryInfo directoryInfo, string extendsClass = null)
626626
{
627627

628-
Console.WriteLine($"Extracting Modular Blocks in {contentTypeName}.");
628+
Console.WriteLine(Messages.ExtractingModularBlocksInContentType(contentTypeName));
629629

630630
// Get modular Block within ContentType
631631
var usingDirectiveList = new List<string>();
@@ -635,7 +635,7 @@ private void CreateModularFile(string contentTypeName, string nameSpace, Content
635635
usingDirectiveList.Add(modularUsingDirective);
636636
}
637637

638-
Console.WriteLine($"Extracting Groups in {contentTypeName}.");
638+
Console.WriteLine(Messages.ExtractingGroupsInContentType(contentTypeName));
639639

640640
string groupUsingDirective = CreateGroup(nameSpace, contentTypeName, contentType.Schema, directoryInfo);
641641
usingDirectiveList.Add(groupUsingDirective);
@@ -699,7 +699,7 @@ private Boolean findRTEReference(List<Field> Schema)
699699
private void CreateFile(string contentTypeName, string nameSpace, Contenttype contentType, DirectoryInfo directoryInfo)
700700
{
701701

702-
Console.WriteLine($"Extracting Modular Blocks in {contentTypeName}.");
702+
Console.WriteLine(Messages.ExtractingModularBlocksInContentType(contentTypeName));
703703

704704
var fields = findRTEReference(contentType.Schema);
705705

@@ -711,7 +711,7 @@ private void CreateFile(string contentTypeName, string nameSpace, Contenttype co
711711
usingDirectiveList.Add(modularUsingDirective);
712712
}
713713

714-
Console.WriteLine($"Extracting Groups in {contentTypeName}.");
714+
Console.WriteLine(Messages.ExtractingGroupsInContentType(contentTypeName));
715715

716716
string groupUsingDirective = CreateGroup(nameSpace, contentTypeName, contentType.Schema, directoryInfo);
717717
usingDirectiveList.Add(groupUsingDirective);
@@ -775,12 +775,12 @@ private FileInfo shouldCreateFile(string fileName, DirectoryInfo directoryInfo)
775775
if (!prompt)
776776
{
777777
Console.ForegroundColor = ConsoleColor.Red;
778-
Console.WriteLine($"Skipping {file.Name}");
778+
Console.WriteLine(Messages.SkippingFile(file.Name));
779779
Console.ResetColor();
780780
return null;
781781
}
782782
}
783-
Console.WriteLine($"Adding File {fileName} to {directoryInfo.FullName}.");
783+
Console.WriteLine(Messages.AddingFile(fileName, directoryInfo.FullName));
784784
return file;
785785
}
786786

@@ -789,7 +789,7 @@ private DirectoryInfo CreateDirectory(string path)
789789
var dir = new DirectoryInfo(path);
790790
if (!dir.Exists)
791791
{
792-
Console.WriteLine($"Path {path} does not exist and will be created.");
792+
Console.WriteLine(Messages.OutputPathNotFound(path));
793793
dir.Create();
794794
}
795795
return dir;
@@ -947,7 +947,7 @@ private void AddEnum(string enumName, in StringBuilder sb)
947947

948948
private void CreateGroupClass(string groupName, string nameSpace, Field field, DirectoryInfo directoryInfo)
949949
{
950-
Console.WriteLine($"Extracting Modular Blocks in {groupName}.");
950+
Console.WriteLine(Messages.ExtractingModularBlocksInGroup(groupName));
951951

952952
// Get modular Block within Group
953953
var usingDirectiveList = new List<string>();
@@ -957,7 +957,7 @@ private void CreateGroupClass(string groupName, string nameSpace, Field field, D
957957
usingDirectiveList.Add(modularUsingDirective);
958958
}
959959

960-
Console.WriteLine($"Extracting Groups in {groupName}.");
960+
Console.WriteLine(Messages.ExtractingGroupsInGroup(groupName));
961961
// Get Group within Group
962962
string grpupUsingDirective = CreateGroup(nameSpace, groupName, field.Schema, directoryInfo);
963963
usingDirectiveList.Add(grpupUsingDirective);

contentstack.model.generator/Program.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
using System;
2-
using System.Threading.Tasks;
1+
using System;
2+
using System.Threading.Tasks;
33
using McMaster.Extensions.CommandLineUtils;
4-
5-
namespace contentstack.model.generator
6-
{
4+
5+
namespace contentstack.model.generator
6+
{
77
class Program
88
{
99
public const int EXCEPTION = 2;
@@ -23,10 +23,11 @@ static async Task<int> Main(string[] args)
2323
catch (Exception ex)
2424
{
2525
Console.ForegroundColor = ConsoleColor.Red;
26-
Console.Error.WriteLine("Unexpected error: " + ex.ToString());
26+
Console.Error.WriteLine(Messages.UnexpectedError);
27+
Console.Error.WriteLine(ex.ToString());
2728
Console.ResetColor();
2829
return EXCEPTION;
2930
}
3031
}
31-
}
32-
}
32+
}
33+
}

contentstack.model.generator/contentstack.model.generator.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@
1313
<PackAsTool>true</PackAsTool>
1414
<PackageOutputPath>./nupkg</PackageOutputPath>
1515
<PackOnBuild>true</PackOnBuild>
16-
<PackageVersion>0.5.0</PackageVersion>
16+
<PackageVersion>0.5.1</PackageVersion>
1717
<Authors>Contentstack</Authors>
18-
<ReleaseVersion>0.5.0</ReleaseVersion>
18+
<ReleaseVersion>0.5.1</ReleaseVersion>
1919
<RootNamespace>Contentstack.Model.Generator</RootNamespace>
2020
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
21-
<PackageReleaseNotes>Modular block with Global field issue resolved</PackageReleaseNotes>
21+
<PackageReleaseNotes>Improved Error messages</PackageReleaseNotes>
2222
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
2323
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
24-
<PackageTags>v0.5.0</PackageTags>
24+
<PackageTags>v0.5.1</PackageTags>
2525
<Configurations>Release;Debug</Configurations>
2626
</PropertyGroup>
2727

0 commit comments

Comments
 (0)