Browse Source

Improving extension adding error messages

Source commit: fb8cb3fe35818705415d31d590aac630f9ddd5ea
Martin Prikryl 9 years ago
parent
commit
f91f665305

+ 1 - 1
source/core/Http.cpp

@@ -123,7 +123,7 @@ void THttp::SendRequest(const char * Method, const UnicodeString & Request)
           const ne_status * NeonStatus = ne_get_status(NeonRequest);
           const ne_status * NeonStatus = ne_get_status(NeonRequest);
           if (NeonStatus->klass != 2)
           if (NeonStatus->klass != 2)
           {
           {
-            throw Exception(FMTLOAD(HTTP_ERROR, (NeonStatus->code, StrFromNeon(NeonStatus->reason_phrase), FHostName)));
+            throw Exception(FMTLOAD(HTTP_ERROR2, (NeonStatus->code, StrFromNeon(NeonStatus->reason_phrase), FHostName)));
           }
           }
 
 
           void * Cursor = NULL;
           void * Cursor = NULL;

+ 53 - 40
source/forms/Preferences.cpp

@@ -2407,46 +2407,72 @@ void __fastcall TPreferencesDialog::AddExtension()
     UnicodeString FileName;
     UnicodeString FileName;
     UnicodeString ExtensionPath;
     UnicodeString ExtensionPath;
     std::unique_ptr<TStringList> Lines(new TStringList());
     std::unique_ptr<TStringList> Lines(new TStringList());
-    if (IsHttpOrHttpsUrl(Path))
+    std::unique_ptr<TCustomCommandType> CustomCommand;
+
+    try
     {
     {
-      bool WinSCPURL = IsWinSCPUrl(Path);
-      if (WinSCPURL)
+      if (IsHttpOrHttpsUrl(Path))
       {
       {
-        if (IsHttpUrl(Path))
+        UnicodeString Url = Path;
+        bool WinSCPURL = IsWinSCPUrl(Url);
+        if (WinSCPURL)
         {
         {
-          Path = ChangeUrlProtocol(Path, HttpsProtocol);
+          if (IsHttpUrl(Url))
+          {
+            Url = ChangeUrlProtocol(Url, HttpsProtocol);
+          }
+          Url = CampaignUrl(ProgramUrl(Url));
         }
         }
-        Path = CampaignUrl(ProgramUrl(Path));
-      }
 
 
-      TOperationVisualizer Visualizer;
+        TOperationVisualizer Visualizer;
 
 
-      std::unique_ptr<THttp> Http(CreateHttp());
-      Http->URL = Path;
-      std::unique_ptr<TStrings> Headers(new TStringList());
-      Headers->Values[L"Accept"] = L"text/winscpextension,text/plain";
-      Http->RequestHeaders = Headers.get();
-      Http->Get();
+        std::unique_ptr<THttp> Http(CreateHttp());
+        Http->URL = Url;
+        std::unique_ptr<TStrings> Headers(new TStringList());
+        Headers->Values[L"Accept"] = L"text/winscpextension,text/plain";
+        Http->RequestHeaders = Headers.get();
+        Http->Get();
 
 
-      UnicodeString TrustedStr = Http->ResponseHeaders->Values["WinSCP-Extension-Trusted"];
-      Trusted = WinSCPURL && (StrToIntDef(TrustedStr, 0) != 0);
+        UnicodeString TrustedStr = Http->ResponseHeaders->Values["WinSCP-Extension-Trusted"];
+        Trusted = WinSCPURL && (StrToIntDef(TrustedStr, 0) != 0);
 
 
-      FileName = MakeValidFileName(Http->ResponseHeaders->Values["WinSCP-Extension-Id"]);
-      if (FileName.IsEmpty())
-      {
-        FileName = MakeValidFileName(ExtractFileNameFromUrl(Path));
+        FileName = MakeValidFileName(Http->ResponseHeaders->Values["WinSCP-Extension-Id"]);
+        if (FileName.IsEmpty())
+        {
+          FileName = MakeValidFileName(ExtractFileNameFromUrl(Path));
+        }
+        Lines->Text = Http->Response;
       }
       }
-      Lines->Text = Http->Response;
-    }
-    else
-    {
-      if (!FileExists(Path))
+      else
       {
       {
-        throw Exception(MainInstructions(FMTLOAD(FILE_NOT_EXISTS, (Path))));
+        if (!FileExists(Path))
+        {
+          throw Exception(MainInstructions(FMTLOAD(FILE_NOT_EXISTS, (Path))));
+        }
+
+        Trusted = true;
+
+        UnicodeString Id = WinConfiguration->GetExtensionId(Path);
+        FileName = ExtractFileName(Path);
+        if (!Id.IsEmpty())
+        {
+          ExtensionPath = Path;
+        }
+
+        LoadScriptFromFile(Path, Lines.get());
       }
       }
 
 
-      Trusted = true;
+      // validate syntax
+      CustomCommand.reset(new TCustomCommandType());
+      CustomCommand->LoadExtension(Lines.get());
+    }
+    catch (Exception & E)
+    {
+      throw ExtException(&E, MainInstructions(FMTLOAD(EXTENSION_LOAD_ERROR, (Path))));
+    }
 
 
+    if (!ExtensionPath.IsEmpty())
+    {
       int Index = FExtensionList->FindIndexByFileName(Path);
       int Index = FExtensionList->FindIndexByFileName(Path);
       if (Index > 0)
       if (Index > 0)
       {
       {
@@ -2454,21 +2480,8 @@ void __fastcall TPreferencesDialog::AddExtension()
         CustomCommandsView->SetFocus();
         CustomCommandsView->SetFocus();
         throw Exception(MainInstructions(LoadStr(EXTENSION_INSTALLED_ALREADY)));
         throw Exception(MainInstructions(LoadStr(EXTENSION_INSTALLED_ALREADY)));
       }
       }
-
-      UnicodeString Id = WinConfiguration->GetExtensionId(Path);
-      FileName = ExtractFileName(Path);
-      if (!Id.IsEmpty())
-      {
-        ExtensionPath = Path;
-      }
-
-      LoadScriptFromFile(Path, Lines.get());
     }
     }
 
 
-    // validate syntax
-    std::unique_ptr<TCustomCommandType> CustomCommand(new TCustomCommandType());
-    CustomCommand->LoadExtension(Lines.get());
-
     if (FExtensionList->Find(CustomCommand->Name) != NULL)
     if (FExtensionList->Find(CustomCommand->Name) != NULL)
     {
     {
       throw Exception(MainInstructions(FMTLOAD(EXTENSION_DUPLICATE, (StripHotkey(CustomCommand->Name)))));
       throw Exception(MainInstructions(FMTLOAD(EXTENSION_DUPLICATE, (StripHotkey(CustomCommand->Name)))));

+ 1 - 1
source/resource/TextsCore.h

@@ -255,7 +255,7 @@
 #define CERT_IP_CANNOT_VERIFY   730
 #define CERT_IP_CANNOT_VERIFY   730
 #define HOSTKEY_NOT_CONFIGURED  731
 #define HOSTKEY_NOT_CONFIGURED  731
 #define UNENCRYPTED_REDIRECT    732
 #define UNENCRYPTED_REDIRECT    732
-#define HTTP_ERROR              733
+#define HTTP_ERROR2             733
 #define FILEZILLA_SITE_MANAGER_NOT_FOUND 734
 #define FILEZILLA_SITE_MANAGER_NOT_FOUND 734
 #define FILEZILLA_NO_SITES      735
 #define FILEZILLA_NO_SITES      735
 #define FILEZILLA_SITE_NOT_EXIST 736
 #define FILEZILLA_SITE_NOT_EXIST 736

+ 1 - 1
source/resource/TextsCore1.rc

@@ -225,7 +225,7 @@ BEGIN
   CERT_IP_CANNOT_VERIFY, "When connecting using an IP address, it is not possible to verify if the certificate was issued for the server. Use a hostname instead of the IP address."
   CERT_IP_CANNOT_VERIFY, "When connecting using an IP address, it is not possible to verify if the certificate was issued for the server. Use a hostname instead of the IP address."
   HOSTKEY_NOT_CONFIGURED, "Expected host key was not configured, use -hostkey switch."
   HOSTKEY_NOT_CONFIGURED, "Expected host key was not configured, use -hostkey switch."
   UNENCRYPTED_REDIRECT, "Redirected to an unencrypted URL."
   UNENCRYPTED_REDIRECT, "Redirected to an unencrypted URL."
-  HTTP_ERROR, "Received response %d %s from %s"
+  HTTP_ERROR2, "Received response %d \"%s\" from %s."
   FILEZILLA_SITE_MANAGER_NOT_FOUND, "FileZilla site manager file not found (%s)."
   FILEZILLA_SITE_MANAGER_NOT_FOUND, "FileZilla site manager file not found (%s)."
   FILEZILLA_NO_SITES, "No sites found in FileZilla site manager file (%s)."
   FILEZILLA_NO_SITES, "No sites found in FileZilla site manager file (%s)."
   FILEZILLA_SITE_NOT_EXIST, "FileZilla site \"%s\" was not found."
   FILEZILLA_SITE_NOT_EXIST, "FileZilla site \"%s\" was not found."

+ 1 - 0
source/resource/TextsWin.h

@@ -90,6 +90,7 @@
 #define EXTENSION_NOT_FOUND     1204
 #define EXTENSION_NOT_FOUND     1204
 #define EXTENSION_DUPLICATE     1205
 #define EXTENSION_DUPLICATE     1205
 #define EXTENSION_INSTALLED_ALREADY 1206
 #define EXTENSION_INSTALLED_ALREADY 1206
+#define EXTENSION_LOAD_ERROR    1207
 
 
 #define WIN_CONFIRMATION_STRINGS 1300
 #define WIN_CONFIRMATION_STRINGS 1300
 #define CONFIRM_OVERWRITE_SESSION 1301
 #define CONFIRM_OVERWRITE_SESSION 1301

+ 1 - 0
source/resource/TextsWin1.rc

@@ -96,6 +96,7 @@ BEGIN
         EXTENSION_DIRECTIVE_MISSING, "Missing mandatory extension directive %s."
         EXTENSION_DIRECTIVE_MISSING, "Missing mandatory extension directive %s."
         EXTENSION_DUPLICATE, "There is already an extension with the name \"%s\"."
         EXTENSION_DUPLICATE, "There is already an extension with the name \"%s\"."
         EXTENSION_INSTALLED_ALREADY, "The extension is installed already."
         EXTENSION_INSTALLED_ALREADY, "The extension is installed already."
+        EXTENSION_LOAD_ERROR, "Error loading an extension from \"%s\"."
 
 
         WIN_CONFIRMATION_STRINGS, "WIN_CONFIRMATION"
         WIN_CONFIRMATION_STRINGS, "WIN_CONFIRMATION"
         CONFIRM_OVERWRITE_SESSION, "Site with name '%s' already exists. Overwrite?"
         CONFIRM_OVERWRITE_SESSION, "Site with name '%s' already exists. Overwrite?"