Exception handling consolidation in command handlers.

Use Context.Output to write error messages when you handle an Exception or an error case. If you can't recover afterwards, just return.
Don't throw an exception from within the handler unless necessary.
-> If you want to throw due to an inner exception, throw an OrchardException. Add an error message and the exception as the innerexception of your OrchardException.
Your exception will be caught by the command executive, an error message will indicate which command failed, and the inner exceptions will be displayed.
Also, do not return values from commands, they are not services.

--HG--
branch : 1.x
extra : transplant_source : %5Ej%123%DA%E0%28%A7l%F5%F9%A4%B0z%05%EB%E4%21%FD%DC
This commit is contained in:
Suha Can
2011-06-10 13:34:29 -07:00
parent 95c0ac69e6
commit dbe908a717
13 changed files with 121 additions and 108 deletions

View File

@@ -32,7 +32,8 @@ namespace Orchard.Packaging.Commands {
public void CreatePackage(string extensionName, string path) {
var packageData = _packageManager.Harvest(extensionName);
if (packageData == null) {
throw new OrchardException(T("Module or Theme \"{0}\" does not exist in this Orchard installation.", extensionName));
Context.Output.WriteLine(T("Module or Theme \"{0}\" does not exist in this Orchard installation.", extensionName));
return;
}
// append "Orchard.[ExtensionType]" to prevent conflicts with other packages (e.g, TinyMce, jQuery, ...)
@@ -62,14 +63,15 @@ namespace Orchard.Packaging.Commands {
public void InstallPackage(string packageId, string location) {
try {
_packageManager.Install(packageId, Version, Path.GetFullPath(location), ApplicationPath);
foreach (var message in _notifier.List()) {
Context.Output.WriteLine(message.Message);
}
}
catch(Exception e) {
// Exceptions area thrown by NuGet as error messages
Context.Output.WriteLine(HttpUtility.HtmlDecode(T("Could not install the package: {0}", e.Message).Text));
return;
}
foreach (var message in _notifier.List()) {
Context.Output.WriteLine(message.Message);
}
}
@@ -83,13 +85,15 @@ namespace Orchard.Packaging.Commands {
try {
_packageManager.Uninstall(packageId, ApplicationPath);
foreach ( var message in _notifier.List() ) {
Context.Output.WriteLine(message.Message);
}
}
catch(Exception e) {
// Exceptions area thrown by NuGet as error messages
throw new OrchardException(T(HttpUtility.HtmlDecode(T("Could not uninstall the package: {0}", e.Message).Text)));
Context.Output.WriteLine(T(HttpUtility.HtmlDecode(T("Could not unintall the package: {0}", e.Message).Text)));
return;
}
foreach (var message in _notifier.List()) {
Context.Output.WriteLine(message.Message);
}
}
}