Skip to content

Commit bd70645

Browse files
Introduced Error response codes when conversion fails.
1 parent 5b4245e commit bd70645

File tree

4 files changed

+23
-30
lines changed

4 files changed

+23
-30
lines changed

ConvertApi.Cli.Tests/CliTests.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace ConvertApi.Cli.Tests;
55
[TestFixture]
66
public class CliTests
77
{
8-
private const string ApiToken = "token_6yNouyuY"; // Provide your API token
8+
private const string ApiToken = "api_token"; // Provide your API token
99
private static readonly string TestOutputDir = Path.Combine(Directory.GetCurrentDirectory(), "test_output");
1010

1111
[SetUp]
@@ -50,7 +50,7 @@ public void TestCompareDocx()
5050
var inputFile2 = Path.Combine(Directory.GetCurrentDirectory(), "../../../../", "test_files", "demo-changed.docx");
5151
var process = RunCli($"{ApiToken} {outputFile} {inputFile1} docx compare CompareFile={inputFile2} storeFile=true");
5252

53-
Assert.AreEqual(0, process.ExitCode, "CLI did not exit cleanly.");
53+
Assert.AreEqual(1, process.ExitCode, "StoreFile parameter is not allowed");
5454
Assert.IsTrue(!File.Exists(outputFile), "Output file should not be not created.");
5555
}
5656

@@ -100,12 +100,9 @@ private Process RunCli(string arguments)
100100
if (!process.WaitForExit(20000))
101101
{
102102
process.Kill();
103-
throw new TimeoutException("The CLI process did not exit within 2 minutes.");
103+
throw new TimeoutException("The CLI process did not exit within 20 s.");
104104
}
105105

106-
if (process.ExitCode != 0)
107-
throw new Exception($"CLI process exited with code {process.ExitCode}");
108-
109106
return process;
110107
}
111108

ConvertApi.Cli.Tests/DirectCallTests.cs

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ namespace ConvertApi.Cli.Tests;
33
[TestFixture]
44
public class DirectCallTests
55
{
6-
private const string ApiToken = "token_6yNouyuY"; // Provide your API token
6+
private const string ApiToken = "api_token"; // Provide your API token
77
private static readonly string TestOutputDir = Path.Combine(Directory.GetCurrentDirectory(), "test_output");
88

99
[SetUp]
@@ -121,29 +121,6 @@ public async Task TestPdfToExtractImages()
121121
var outputFiles = Directory.GetFiles(TestOutputDir);
122122
Assert.That(outputFiles, Has.Length.EqualTo(1), "Output file was not created.");
123123
}
124-
125-
[Test]
126-
public async Task TestPdfToExtractImagesError()
127-
{
128-
var inputFile = Path.Combine(Directory.GetCurrentDirectory(), "../../../../", "test_files", "22pages.pdf");
129-
130-
await Program.Main([ApiToken, TestOutputDir, inputFile, "pdf", "extract-images"]);
131-
132-
var outputFiles = Directory.GetFiles(TestOutputDir);
133-
Assert.That(outputFiles, Has.Length.EqualTo(0), "There should not be any converted files.");
134-
}
135-
136-
[Test]
137-
public async Task TestPdfToDocxWithMixedUpInputs()
138-
{
139-
var inputFile = Path.Combine(Directory.GetCurrentDirectory(), "../../../../", "test_files", "simple.pdf");
140-
141-
// pdf and docx are mixed on purpose!
142-
await Program.Main([ApiToken, TestOutputDir, inputFile, "docx", "pdf"]);
143-
144-
var outputFiles = Directory.GetFiles(TestOutputDir);
145-
Assert.That(outputFiles, Has.Length.EqualTo(0), "There should not be any converted files.");
146-
}
147124

148125
[Test]
149126
public async Task TestWebToPdf()

ConvertApi.Cli/Program.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public static async Task Main(string[] args)
5151
if (dynamicProperties.Any(x => x.Key.ToLower() == "storefile" && x.Value.ToLower() == "true"))
5252
{
5353
Console.WriteLine("Error: StoreFile parameter is not allowed in cli tool. Please use our API directly if you need this parameter.");
54+
Environment.Exit(1);
5455
return;
5556
}
5657

@@ -69,6 +70,7 @@ private static void ValidateInputFiles(string[] inputFiles, string fromFormat)
6970
if (inputFiles.Length == 0)
7071
{
7172
Console.WriteLine("Error: At least one input file is required.");
73+
Environment.Exit(1);
7274
return;
7375
}
7476

@@ -77,6 +79,7 @@ private static void ValidateInputFiles(string[] inputFiles, string fromFormat)
7779
if (!File.Exists(file))
7880
{
7981
Console.WriteLine($"Error: Input file not found: {file}");
82+
Environment.Exit(1);
8083
return;
8184
}
8285
}
@@ -107,6 +110,11 @@ static void DisplayHelp()
107110
Console.WriteLine("Dynamic Properties:");
108111
Console.WriteLine(" Specify additional parameters using key=value pairs, such as UserPassword, OwnerPassword, or WatermarkText, depending on the API being used.");
109112
Console.WriteLine();
113+
Console.WriteLine("Exit Codes:");
114+
Console.WriteLine(" 0 - Success");
115+
Console.WriteLine(" 1 - Error in arguments/parameters validation");
116+
Console.WriteLine(" 2 - API response error");
117+
Console.WriteLine();
110118
Console.WriteLine("For more information, visit: https://www.convertapi.com/");
111119
}
112120

@@ -184,6 +192,7 @@ static async Task ConvertFiles(string apiToken, string fromFormat, string toForm
184192
{
185193
var errorContent = await response.Content.ReadAsStringAsync();
186194
Console.WriteLine($"Error: {response.StatusCode}. Response message: {errorContent}");
195+
Environment.Exit(2);
187196
}
188197
}
189198
}

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,16 @@ Add watermark-overlay to a PDF:
129129
convertapi-cli.exe YOUR_API_TOKEN output_directory input.pdf pdf watermark-overlay OverlayFile=overlay-file.pdf FileName=watermarkResult
130130
```
131131

132+
## Exit Codes
133+
134+
The ConvertAPI CLI uses exit codes to indicate the outcome of a command execution:
135+
136+
- **0** - Success: The command completed without errors.
137+
- **1** - Argument/Parameter Validation Error: This occurs when there are issues with the arguments, such as insufficient parameters or invalid file paths.
138+
- **2** - API Response Error: This occurs when the API call fails.
139+
140+
Use these exit codes to programmatically detect failures and handle them appropriately in scripts or integrations.
141+
132142

133143
### Issues & Comments
134144
Please leave all comments, bugs, requests, and issues on the Issues page. We'll respond to your request ASAP!

0 commit comments

Comments
 (0)