From c13815810e8ff427614ca82fe0dfd4b1e773f0f3 Mon Sep 17 00:00:00 2001 From: Steve Lamerton Date: Sun, 30 Dec 2012 20:19:58 +0000 Subject: [PATCH] Add handling of http errors to wxWebViewIE. Tidy up existing large case statement. Closes #14877. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@73325 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- samples/webview/webview.cpp | 52 ++++------- src/msw/webview_ie.cpp | 181 +++++++++++++----------------------- 2 files changed, 82 insertions(+), 151 deletions(-) diff --git a/samples/webview/webview.cpp b/samples/webview/webview.cpp index b15b1d8119..8589f4cbce 100644 --- a/samples/webview/webview.cpp +++ b/samples/webview/webview.cpp @@ -965,48 +965,30 @@ void WebFrame::OnSelectAll(wxCommandEvent& WXUNUSED(evt)) * Callback invoked when a loading error occurs */ void WebFrame::OnError(wxWebViewEvent& evt) -{ - wxString errorCategory; +{ +#define WX_ERROR_CASE(type) \ + case type: \ + category = #type; \ + break; + + wxString category; switch (evt.GetInt()) { - case wxWEB_NAV_ERR_CONNECTION: - errorCategory = "wxWEB_NAV_ERR_CONNECTION"; - break; - - case wxWEB_NAV_ERR_CERTIFICATE: - errorCategory = "wxWEB_NAV_ERR_CERTIFICATE"; - break; - - case wxWEB_NAV_ERR_AUTH: - errorCategory = "wxWEB_NAV_ERR_AUTH"; - break; - - case wxWEB_NAV_ERR_SECURITY: - errorCategory = "wxWEB_NAV_ERR_SECURITY"; - break; - - case wxWEB_NAV_ERR_NOT_FOUND: - errorCategory = "wxWEB_NAV_ERR_NOT_FOUND"; - break; - - case wxWEB_NAV_ERR_REQUEST: - errorCategory = "wxWEB_NAV_ERR_REQUEST"; - break; - - case wxWEB_NAV_ERR_USER_CANCELLED: - errorCategory = "wxWEB_NAV_ERR_USER_CANCELLED"; - break; - - case wxWEB_NAV_ERR_OTHER: - errorCategory = "wxWEB_NAV_ERR_OTHER"; - break; + WX_ERROR_CASE(wxWEB_NAV_ERR_CONNECTION); + WX_ERROR_CASE(wxWEB_NAV_ERR_CERTIFICATE); + WX_ERROR_CASE(wxWEB_NAV_ERR_AUTH); + WX_ERROR_CASE(wxWEB_NAV_ERR_SECURITY); + WX_ERROR_CASE(wxWEB_NAV_ERR_NOT_FOUND); + WX_ERROR_CASE(wxWEB_NAV_ERR_REQUEST); + WX_ERROR_CASE(wxWEB_NAV_ERR_USER_CANCELLED); + WX_ERROR_CASE(wxWEB_NAV_ERR_OTHER); } - wxLogMessage("%s", "Error; url='" + evt.GetURL() + "', error='" + errorCategory + "' (" + evt.GetString() + ")"); + wxLogMessage("%s", "Error; url='" + evt.GetURL() + "', error='" + category + " (" + evt.GetString() + ")'"); //Show the info bar with an error m_info->ShowMessage(_("An error occurred loading ") + evt.GetURL() + "\n" + - "'" + errorCategory + "' (" + evt.GetString() + ")", wxICON_ERROR); + "'" + category + "'", wxICON_ERROR); UpdateState(); } diff --git a/src/msw/webview_ie.cpp b/src/msw/webview_ie.cpp index 94c708ec64..2f6ac73c3d 100644 --- a/src/msw/webview_ie.cpp +++ b/src/msw/webview_ie.cpp @@ -28,6 +28,7 @@ #include "wx/filesys.h" #include "wx/dynlib.h" #include +#include /* These GUID definitions are our own implementation to support interfaces * normally in urlmon.h. See include/wx/msw/webview_ie.h @@ -50,6 +51,13 @@ enum //Internal find flags } +//Convenience function for error conversion +#define WX_ERROR_CASE(error, wxerror) \ + case error: \ + event.SetString(#error); \ + event.SetInt(wxerror); \ + break; + wxIMPLEMENT_DYNAMIC_CLASS(wxWebViewIE, wxWebView); BEGIN_EVENT_TABLE(wxWebViewIE, wxControl) @@ -1321,125 +1329,66 @@ void wxWebViewIE::onActiveXEvent(wxActiveXEvent& evt) case DISPID_NAVIGATEERROR: { - wxWebViewNavigationError errorType = wxWEB_NAV_ERR_OTHER; - wxString errorCode = "?"; + wxWebViewEvent event(wxEVT_COMMAND_WEB_VIEW_ERROR, GetId(), + evt[1].GetString(), evt[2].GetString()); + event.SetEventObject(this); + switch (evt[3].GetLong()) { - case INET_E_INVALID_URL: // (0x800C0002L or -2146697214) - errorCode = "INET_E_INVALID_URL"; - errorType = wxWEB_NAV_ERR_REQUEST; - break; - case INET_E_NO_SESSION: // (0x800C0003L or -2146697213) - errorCode = "INET_E_NO_SESSION"; - errorType = wxWEB_NAV_ERR_CONNECTION; - break; - case INET_E_CANNOT_CONNECT: // (0x800C0004L or -2146697212) - errorCode = "INET_E_CANNOT_CONNECT"; - errorType = wxWEB_NAV_ERR_CONNECTION; - break; - case INET_E_RESOURCE_NOT_FOUND: // (0x800C0005L or -2146697211) - errorCode = "INET_E_RESOURCE_NOT_FOUND"; - errorType = wxWEB_NAV_ERR_NOT_FOUND; - break; - case INET_E_OBJECT_NOT_FOUND: // (0x800C0006L or -2146697210) - errorCode = "INET_E_OBJECT_NOT_FOUND"; - errorType = wxWEB_NAV_ERR_NOT_FOUND; - break; - case INET_E_DATA_NOT_AVAILABLE: // (0x800C0007L or -2146697209) - errorCode = "INET_E_DATA_NOT_AVAILABLE"; - errorType = wxWEB_NAV_ERR_NOT_FOUND; - break; - case INET_E_DOWNLOAD_FAILURE: // (0x800C0008L or -2146697208) - errorCode = "INET_E_DOWNLOAD_FAILURE"; - errorType = wxWEB_NAV_ERR_CONNECTION; - break; - case INET_E_AUTHENTICATION_REQUIRED: // (0x800C0009L or -2146697207) - errorCode = "INET_E_AUTHENTICATION_REQUIRED"; - errorType = wxWEB_NAV_ERR_AUTH; - break; - case INET_E_NO_VALID_MEDIA: // (0x800C000AL or -2146697206) - errorCode = "INET_E_NO_VALID_MEDIA"; - errorType = wxWEB_NAV_ERR_REQUEST; - break; - case INET_E_CONNECTION_TIMEOUT: // (0x800C000BL or -2146697205) - errorCode = "INET_E_CONNECTION_TIMEOUT"; - errorType = wxWEB_NAV_ERR_CONNECTION; - break; - case INET_E_INVALID_REQUEST: // (0x800C000CL or -2146697204) - errorCode = "INET_E_INVALID_REQUEST"; - errorType = wxWEB_NAV_ERR_REQUEST; - break; - case INET_E_UNKNOWN_PROTOCOL: // (0x800C000DL or -2146697203) - errorCode = "INET_E_UNKNOWN_PROTOCOL"; - errorType = wxWEB_NAV_ERR_REQUEST; - break; - case INET_E_SECURITY_PROBLEM: // (0x800C000EL or -2146697202) - errorCode = "INET_E_SECURITY_PROBLEM"; - errorType = wxWEB_NAV_ERR_SECURITY; - break; - case INET_E_CANNOT_LOAD_DATA: // (0x800C000FL or -2146697201) - errorCode = "INET_E_CANNOT_LOAD_DATA"; - errorType = wxWEB_NAV_ERR_OTHER; - break; - case INET_E_CANNOT_INSTANTIATE_OBJECT: - // CoCreateInstance will return an error code if this happens, - // we'll handle this above. - return; - break; - case INET_E_REDIRECT_FAILED: // (0x800C0014L or -2146697196) - errorCode = "INET_E_REDIRECT_FAILED"; - errorType = wxWEB_NAV_ERR_OTHER; - break; - case INET_E_REDIRECT_TO_DIR: // (0x800C0015L or -2146697195) - errorCode = "INET_E_REDIRECT_TO_DIR"; - errorType = wxWEB_NAV_ERR_REQUEST; - break; - case INET_E_CANNOT_LOCK_REQUEST: // (0x800C0016L or -2146697194) - errorCode = "INET_E_CANNOT_LOCK_REQUEST"; - errorType = wxWEB_NAV_ERR_OTHER; - break; - case INET_E_USE_EXTEND_BINDING: // (0x800C0017L or -2146697193) - errorCode = "INET_E_USE_EXTEND_BINDING"; - errorType = wxWEB_NAV_ERR_OTHER; - break; - case INET_E_TERMINATED_BIND: // (0x800C0018L or -2146697192) - errorCode = "INET_E_TERMINATED_BIND"; - errorType = wxWEB_NAV_ERR_OTHER; - break; - case INET_E_INVALID_CERTIFICATE: // (0x800C0019L or -2146697191) - errorCode = "INET_E_INVALID_CERTIFICATE"; - errorType = wxWEB_NAV_ERR_CERTIFICATE; - break; - case INET_E_CODE_DOWNLOAD_DECLINED: // (0x800C0100L or -2146696960) - errorCode = "INET_E_CODE_DOWNLOAD_DECLINED"; - errorType = wxWEB_NAV_ERR_USER_CANCELLED; - break; - case INET_E_RESULT_DISPATCHED: // (0x800C0200L or -2146696704) - // cancel request cancelled... - errorCode = "INET_E_RESULT_DISPATCHED"; - errorType = wxWEB_NAV_ERR_OTHER; - break; - case INET_E_CANNOT_REPLACE_SFP_FILE: // (0x800C0300L or -2146696448) - errorCode = "INET_E_CANNOT_REPLACE_SFP_FILE"; - errorType = wxWEB_NAV_ERR_SECURITY; - break; - case INET_E_CODE_INSTALL_BLOCKED_BY_HASH_POLICY: - errorCode = "INET_E_CODE_INSTALL_BLOCKED_BY_HASH_POLICY"; - errorType = wxWEB_NAV_ERR_SECURITY; - break; - case INET_E_CODE_INSTALL_SUPPRESSED: - errorCode = "INET_E_CODE_INSTALL_SUPPRESSED"; - errorType = wxWEB_NAV_ERR_SECURITY; - break; - } + // 400 Error codes + WX_ERROR_CASE(HTTP_STATUS_BAD_REQUEST, wxWEB_NAV_ERR_REQUEST) + WX_ERROR_CASE(HTTP_STATUS_DENIED, wxWEB_NAV_ERR_AUTH) + WX_ERROR_CASE(HTTP_STATUS_PAYMENT_REQ, wxWEB_NAV_ERR_OTHER) + WX_ERROR_CASE(HTTP_STATUS_FORBIDDEN, wxWEB_NAV_ERR_AUTH) + WX_ERROR_CASE(HTTP_STATUS_NOT_FOUND, wxWEB_NAV_ERR_NOT_FOUND) + WX_ERROR_CASE(HTTP_STATUS_BAD_METHOD, wxWEB_NAV_ERR_REQUEST) + WX_ERROR_CASE(HTTP_STATUS_NONE_ACCEPTABLE, wxWEB_NAV_ERR_OTHER) + WX_ERROR_CASE(HTTP_STATUS_PROXY_AUTH_REQ, wxWEB_NAV_ERR_AUTH) + WX_ERROR_CASE(HTTP_STATUS_REQUEST_TIMEOUT, wxWEB_NAV_ERR_CONNECTION) + WX_ERROR_CASE(HTTP_STATUS_CONFLICT, wxWEB_NAV_ERR_REQUEST) + WX_ERROR_CASE(HTTP_STATUS_GONE, wxWEB_NAV_ERR_NOT_FOUND) + WX_ERROR_CASE(HTTP_STATUS_LENGTH_REQUIRED, wxWEB_NAV_ERR_REQUEST) + WX_ERROR_CASE(HTTP_STATUS_PRECOND_FAILED, wxWEB_NAV_ERR_REQUEST) + WX_ERROR_CASE(HTTP_STATUS_REQUEST_TOO_LARGE, wxWEB_NAV_ERR_REQUEST) + WX_ERROR_CASE(HTTP_STATUS_URI_TOO_LONG, wxWEB_NAV_ERR_REQUEST) + WX_ERROR_CASE(HTTP_STATUS_UNSUPPORTED_MEDIA, wxWEB_NAV_ERR_REQUEST) + WX_ERROR_CASE(HTTP_STATUS_RETRY_WITH, wxWEB_NAV_ERR_OTHER) - wxString url = evt[1].GetString(); - wxString target = evt[2].GetString(); - wxWebViewEvent event(wxEVT_COMMAND_WEB_VIEW_ERROR, GetId(), - url, target); - event.SetEventObject(this); - event.SetInt(errorType); - event.SetString(errorCode); + // 500 - Error codes + WX_ERROR_CASE(HTTP_STATUS_SERVER_ERROR, wxWEB_NAV_ERR_CONNECTION) + WX_ERROR_CASE(HTTP_STATUS_NOT_SUPPORTED, wxWEB_NAV_ERR_CONNECTION) + WX_ERROR_CASE(HTTP_STATUS_BAD_GATEWAY, wxWEB_NAV_ERR_CONNECTION) + WX_ERROR_CASE(HTTP_STATUS_SERVICE_UNAVAIL, wxWEB_NAV_ERR_CONNECTION) + WX_ERROR_CASE(HTTP_STATUS_GATEWAY_TIMEOUT, wxWEB_NAV_ERR_CONNECTION) + WX_ERROR_CASE(HTTP_STATUS_VERSION_NOT_SUP, wxWEB_NAV_ERR_REQUEST) + + // URL Moniker error codes + WX_ERROR_CASE(INET_E_INVALID_URL, wxWEB_NAV_ERR_REQUEST) + WX_ERROR_CASE(INET_E_NO_SESSION, wxWEB_NAV_ERR_CONNECTION) + WX_ERROR_CASE(INET_E_CANNOT_CONNECT, wxWEB_NAV_ERR_CONNECTION) + WX_ERROR_CASE(INET_E_RESOURCE_NOT_FOUND, wxWEB_NAV_ERR_NOT_FOUND) + WX_ERROR_CASE(INET_E_OBJECT_NOT_FOUND, wxWEB_NAV_ERR_NOT_FOUND) + WX_ERROR_CASE(INET_E_DATA_NOT_AVAILABLE, wxWEB_NAV_ERR_NOT_FOUND) + WX_ERROR_CASE(INET_E_DOWNLOAD_FAILURE, wxWEB_NAV_ERR_CONNECTION) + WX_ERROR_CASE(INET_E_AUTHENTICATION_REQUIRED, wxWEB_NAV_ERR_AUTH) + WX_ERROR_CASE(INET_E_NO_VALID_MEDIA, wxWEB_NAV_ERR_REQUEST) + WX_ERROR_CASE(INET_E_CONNECTION_TIMEOUT, wxWEB_NAV_ERR_CONNECTION) + WX_ERROR_CASE(INET_E_INVALID_REQUEST, wxWEB_NAV_ERR_REQUEST) + WX_ERROR_CASE(INET_E_UNKNOWN_PROTOCOL, wxWEB_NAV_ERR_REQUEST) + WX_ERROR_CASE(INET_E_SECURITY_PROBLEM, wxWEB_NAV_ERR_SECURITY) + WX_ERROR_CASE(INET_E_CANNOT_LOAD_DATA, wxWEB_NAV_ERR_OTHER) + WX_ERROR_CASE(INET_E_REDIRECT_FAILED, wxWEB_NAV_ERR_OTHER) + WX_ERROR_CASE(INET_E_REDIRECT_TO_DIR, wxWEB_NAV_ERR_REQUEST) + WX_ERROR_CASE(INET_E_CANNOT_LOCK_REQUEST, wxWEB_NAV_ERR_OTHER) + WX_ERROR_CASE(INET_E_USE_EXTEND_BINDING, wxWEB_NAV_ERR_OTHER) + WX_ERROR_CASE(INET_E_TERMINATED_BIND, wxWEB_NAV_ERR_OTHER) + WX_ERROR_CASE(INET_E_INVALID_CERTIFICATE, wxWEB_NAV_ERR_CERTIFICATE) + WX_ERROR_CASE(INET_E_CODE_DOWNLOAD_DECLINED, wxWEB_NAV_ERR_USER_CANCELLED) + WX_ERROR_CASE(INET_E_RESULT_DISPATCHED, wxWEB_NAV_ERR_OTHER) + WX_ERROR_CASE(INET_E_CANNOT_REPLACE_SFP_FILE, wxWEB_NAV_ERR_SECURITY) + WX_ERROR_CASE(INET_E_CODE_INSTALL_BLOCKED_BY_HASH_POLICY, wxWEB_NAV_ERR_SECURITY) + WX_ERROR_CASE(INET_E_CODE_INSTALL_SUPPRESSED, wxWEB_NAV_ERR_SECURITY) + } HandleWindowEvent(event); break; }