Skip to content

Commit

Permalink
Merge pull request #36 from Elagoht:feat/descriptive-error-codes
Browse files Browse the repository at this point in the history
Feat/descriptive-error-codes
  • Loading branch information
Elagoht authored Jun 28, 2024
2 parents 6725074 + 21945bd commit 5426e17
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 35 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ We welcome contributions! Please submit a pull request or create an issue to dis

This project is licensed under the GNU General Public License v3.0. For more details, see the [LICENSE](LICENSE) file in the project root.

## Documentation

Document files are located in [docs/](docs) directory.

---

Feel free to reach out if you have any questions or need further assistance. Thank you for using Passenger!
23 changes: 23 additions & 0 deletions docs/EXIT_CODES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Exit Codes

Exit codes are required to specify specific exit situations. This application is design to be implemented to a HTTP server. So decied to make exit codes look like HTTP status codes:

| Status | Exit Code | Referring to HTTP |
|--------------------------|-----------|-------------------|
| Success | must be 0 | 200 |
| Created | must be 0 | 201 |
| See Other | 33 | 303 |
| Bad Request | 40 | 400 |
| Unauthorized | 41 | 401 |
| Forbidden | 43 | 403 |
| Not Found | 44 | 404 |
| Not Acceptable | 46 | 406 |
| Conflict | 49 | 409 |
| Unsupported Media Type | 45 | 415 |
| Internal Server Error | 50 | 500 |

If you want to know more about HTTP status codes, you can visit [here](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status).

## Ask for Help

Help wanted! If you can map every verb command to their exit codes, edit this file and please create a pull request. Thank you!
61 changes: 26 additions & 35 deletions src/Presentation/Error.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,110 +6,110 @@ public static class Error
{
public static void ArgumentCount(string command, int minOrActual, int max = -1)
{
if (minOrActual == 0)
if (minOrActual == 2)
Console.WriteLine($"passenger: {command}: takes no arguments");
if (max == -1)
Console.WriteLine($"passenger: {command}: expected exact {minOrActual} arguments");
else
Console.WriteLine($"passenger: {command}: expected {minOrActual}-{max} arguments");
AskForHelp();
Environment.Exit(2);
Environment.Exit(40);
}

public static void MissingCommand()
{
Console.WriteLine("passenger: missing command");
Environment.Exit(1);
Environment.Exit(40);
}

public static void UnknownCommand()
{
Console.WriteLine("passenger: unknown command");
Environment.Exit(1);
Environment.Exit(40);
}

public static void InvalidToken()
{
Console.WriteLine("passenger: invalid token");
Environment.Exit(1);
Environment.Exit(41);
}

public static void SecretKeyNotProvided()
{
Console.WriteLine("passenger: secret key not provided");
Environment.Exit(1);
Environment.Exit(41);
}

public static void DatabaseLoadFailed()
{
Console.WriteLine("passenger: failed to load or decrypt database");
Environment.Exit(1);
Environment.Exit(50);
}

public static void MissingField(string field)
{
Console.WriteLine($"passenger: missing field '{field}'");
Environment.Exit(1);
Environment.Exit(41);
}

public static void ConstantExists(ConstantPair constant)
{
Console.WriteLine($"passenger: constant '{constant.Key}' already exists");
Environment.Exit(1);
Environment.Exit(49);
}

public static void EntryNotFound()
{
Console.WriteLine("passenger: entry not found");
Environment.Exit(1);
Environment.Exit(44);
}

public static void JsonParseError()
{
Console.WriteLine("passenger: JSON parse error");
Environment.Exit(1);
Environment.Exit(46);
}

public static void AskForHelp()
{
Console.WriteLine("passenger: try 'passenger --help' for more information");
Environment.Exit(1);
Environment.Exit(33);
}

public static void FoundOnRepository()
{
Console.WriteLine("passenger: your password is on a brute-force repository, not saved");
Environment.Exit(1);
Environment.Exit(43);
}

public static void PassphraseTooShort()
{
Console.WriteLine("passenger: passphrase must be at least 8 characters long");
Environment.Exit(1);
Environment.Exit(46);
}

public static void PassphraseTooLong()
{
Console.WriteLine("passenger: more than 4096 characters passphrases are not supported");
Environment.Exit(1);
Environment.Exit(46);
}

public static void BrowserTypeNotSupported()
{
Console.WriteLine("passenger: browser type not supported");
Environment.Exit(1);
Environment.Exit(45);
}

public static void ExportTypeNotSupported()
{
Console.WriteLine("passenger: export type not supported");
Environment.Exit(1);
Environment.Exit(45);
}

public static void ImportFailed()
{
Console.WriteLine("passenger: failed to import data");
Environment.Exit(1);
Environment.Exit(50);
}

public static void ImportSkippedEntries(int count)
Expand All @@ -118,28 +118,19 @@ public static void ImportSkippedEntries(int count)
? $"{count} entries"
: "1 entry"
)} entries skipped due to short password");
Environment.Exit(1);
}

public static void ImportDenied(int count)
{
Console.WriteLine($"passenger: import denied due to {(count > 1
? $"{count} entries"
: "1 entry"
)} with short password");
Environment.Exit(1);
Environment.Exit(40);
}

public static void PipedInputRequired()
{
Console.WriteLine("passenger: Input not provided");
Environment.Exit(1);
Environment.Exit(40);
}

public static void CSVFormatMissmatch()
{
Console.WriteLine("passenger: CSV format mismatched with the specified browser");
Environment.Exit(1);
Environment.Exit(40);
}

public static void FileExceptions(Exception exception)
Expand All @@ -148,19 +139,19 @@ public static void FileExceptions(Exception exception)
{
case FileNotFoundException:
Console.WriteLine("passenger: data file not found");
Environment.Exit(1); break;
Environment.Exit(50); break;
case UnauthorizedAccessException:
Console.WriteLine("passenger: data file access denied");
Environment.Exit(126); break;
Environment.Exit(43); break;
case IOException:
Console.WriteLine("passenger: data file input/output error");
Environment.Exit(1); break;
Environment.Exit(50); break;
case AuthenticationTagMismatchException:
Console.WriteLine("passenger: Authentication cannot be verified");
Environment.Exit(1); break;
Environment.Exit(41); break;
default:
Console.WriteLine("passenger: unexpected error while accessing data file");
Environment.Exit(2); break;
Environment.Exit(50); break;
}
}
}
Expand Down

0 comments on commit 5426e17

Please sign in to comment.