~alcinnz/hurl

ref: 7cea1667ec30438e60daf696347174dccda8dee3 hurl/src/Network/URI/Messages.hs -rw-r--r-- 10.3 KiB
7cea1667 — Adrian Cochrane Localize HTTP errorcodes! 1 year, 8 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
{-# LANGUAGE CPP #-}
-- | Module holding localized error messages to be presented as a response.
--
-- To localize error messages provided by HURL, provide your translations between
-- "BEGIN LOCALIZATION" & "END LOCALIZATION" in this file.
--
-- The lines are formatted:
--    trans ("LANG":_) (KEY) = "TRANSLATION"
-- with uppercase indicating the bits you fill in.
--
-- Translations between #if WITH_HTTP_URI & #endif are specific to HTTP error handling.
module Network.URI.Messages (trans, Errors(..)) where

import Data.List (stripPrefix)
import Data.Maybe (fromMaybe)

#if WITH_HTTP_URI
import Network.HTTP.Client (HttpException(..), HttpExceptionContent(..))
import Control.Exception (displayException)
import Network.TLS (TLSException(..), TLSError(..), AlertDescription(..))
import Control.Exception.Base (fromException)
#endif

trans _ (RawXML markup) = markup
--- BEGIN LOCALIZATION
trans ("en":_) (UnsupportedScheme scheme) = "Unsupported protocol " ++ scheme
trans ("en":_) (UnsupportedMIME mime) = "Unsupported filetype " ++ mime
trans ("en":_) (RequiresInstall mime appsMarkup) =
    "<h1>Please install a compatible app to open <code>" ++ linkType ++ "</code> links</h1>\n" ++ appsMarkup
  where linkType = fromMaybe mime $ stripPrefix "x-scheme-handler/" mime
trans ("en":_) (OpenedWith app) = "Opened in " ++ app
trans ("en":_) (ReadFailed msg) = "Failed to read file: " ++ msg
trans ("en":_) MalformedResponse = "Invalid response!"
trans ("en":_) ExcessiveRedirects = "Too many redirects!"
#if WITH_HTTP_URI
trans ("en":_) (Http (InvalidUrlException url msg)) = "Invalid URL " ++ url ++ ": " ++ msg
trans ("en":_) (Http (HttpExceptionRequest _ (TooManyRedirects _))) = "Too many redirects!"
trans ("en":_) (Http (HttpExceptionRequest _ ResponseTimeout)) = "The site took too long to respond!"
trans ("en":_) (Http (HttpExceptionRequest _ ConnectionTimeout)) = "The site took too long to connect!"
trans ("en":_) (Http (HttpExceptionRequest _ (ConnectionFailure err))) = "Could not connect: " ++ displayException err
trans ("en":_) (Http (HttpExceptionRequest _ (InternalException e))) = case fromException e of
    Just (Terminated _ why _) -> "Secure session disconnected! <em>" ++ why ++ "</em>"
    Just (HandshakeFailed (Error_Misc msg)) ->
        "Failed to establish secure connection! <em>" ++ msg ++ "</em>"
    Just (HandshakeFailed (Error_Protocol (_, _, CloseNotify))) ->
        "Secure session disconnected!"
    Just (HandshakeFailed (Error_Protocol (_, _, HandshakeFailure))) ->
        "Failed to negotiate security parameters!"
    Just (HandshakeFailed (Error_Protocol (_, _, BadCertificate))) ->
        "<h1>The site failed to prove it is who it says it is!</h1>"
    Just (HandshakeFailed (Error_Protocol (_, _, UnsupportedCertificate))) -> unlines [
        "<h1>The site failed to prove it is who it says it is!</h1>",
        "<p>It has sent us a cryptographic certificate I failed to make sense of.</p>"
      ]
    Just (HandshakeFailed (Error_Protocol (_, _, CertificateExpired))) -> unlines [
        "<h1>The site failed to prove it is who it says it is!</h1>",
        "<p>The cryptographic certificate it has sent to us has expired!</p>"
      ]
    Just (HandshakeFailed (Error_Protocol (_, _, CertificateRevoked))) -> unlines [
        "<h1>The site failed to prove it is who it says it is!</h1>",
        "<p>The cryptographic certificate it has sent us has been revoked!</p>"
      ]
    Just (HandshakeFailed (Error_Protocol (_, _, CertificateUnknown))) -> unlines [
        "<h1>The site failed to prove it is who it says it is!</h1>",
        "<p>The cryptographic certificate it has sent us belongs to someone else!</p>"
      ]
    Just (HandshakeFailed (Error_Protocol (_, _, UnknownCa))) -> unlines [
        "<h1>The site failed to prove it is who it says it is!</h1>",
        "<p>The authority vouching for it is unknown to me!</p>"
      ]
    Just (HandshakeFailed (Error_Protocol (why, _, _desc))) ->
        "Failed to establish secure connection! <em>" ++ why ++ "</em>"
    Just (HandshakeFailed (Error_Certificate why)) -> unlines [
        "<h1>The site failed to prove it is who it says it is!</h1>",
        "<p>" ++ why ++ "</p>"
      ]
    Just (HandshakeFailed (Error_HandshakePolicy why)) ->
        "Invalid handshake policy: <em>" ++ why ++ "</em>"
    Just (HandshakeFailed Error_EOF) -> "Secure session disconnected!"
    Just (HandshakeFailed (Error_Packet why)) ->
        "Invalid security packet: <em>" ++ why ++ "</em>"
    Just (HandshakeFailed (Error_Packet_unexpected a b)) -> unlines [
        "<p>Invalid security packet: <em>" ++ a ++ "</em></p>",
        "<p>" ++ b ++ "</p>"
      ]
    Just (HandshakeFailed (Error_Packet_Parsing why)) ->
        "Invalid security packet: <em>" ++ why ++ "</em>"
    Just ConnectionNotEstablished ->
        "Attempted to send or recieve data before establishing secure connection!"
    Nothing -> "Internal error: " ++ displayException e
#endif
trans ("en":_) (GeminiError '1' '1' label) =
    "<form><label>" ++ label ++ "<input type=password></form>" 
trans ("en":_) (GeminiError '1' _ label) = "<form><label>" ++ label ++ "<input></form>"
trans ("en":_) (GeminiError '4' '1' _) = "Site unavailable!"
trans ("en":_) (GeminiError '4' '2' _) = "Program error!"
trans ("en":_) (GeminiError '4' '3' _) = "Proxy error!"
trans ("en":_) (GeminiError '4' '4' timeout) =
    "Site busy! Please reload after at least " ++ timeout ++ " seconds"
trans ("en":_) (GeminiError '5' '1' _) = "Page not found! Try the <a href='/'>homepage</a>."
trans ("en":_) (GeminiError '5' '2' _) = "Page deleted! Try the <a href='/'>homepage</a>."
trans ("en":_) (GeminiError '5' '3' _) = "Contacted wrong server!"
trans ("en":_) (GeminiError '5' '9' _) = "Malformed request, my bad!"
trans ("en":_) (GeminiError '6' '1' _) = "<form><label>Authentication required" ++
    "<input type='-argo-keypair' -argo-error='Unauthorized account!'></form>"
trans ("en":_) (GeminiError '6' '2' _) = "<form><label>Authentication required" ++
    "<input type='-argo-keypair' -argo-error='Invalid account!'></form>"
trans ("en":_) (GeminiError '6' _ _) = "<form><label>Authentication required" ++
    "<input type='-argo-keypair' -argo-error='Invalid account!'></form>"
trans ("en":_) (GeminiError _ _ error) = error
trans ("en":_) (HTTPStatus 400 _) = "I sent a bad request, according to this site."
trans ("en":_) (HTTPStatus 401 _) = "Authentication required!" -- FIXME: Support HTTP Basic Auth.
trans ("en":_) (HTTPStatus 402 _) = "Payment required!"
trans ("en":_) (HTTPStatus 403 _) = "Access denied!"
trans ("en":_) (HTTPStatus 404 _) = "Page not found! Try the <a href='/'>homepage</a>."
trans ("en":_) (HTTPStatus 405 _) = "Bad webform for this destination webaddress! " ++
    "<em>Method not allowed</em>."
trans ("en":_) (HTTPStatus 406 _) = "No representation available for given criteria!"
trans ("en":_) (HTTPStatus 407 _) = "Authentication into proxyserver required!"
trans ("en":_) (HTTPStatus 408 _) = "The site took too long to connect! <em>(HTTP 408)</em>"
trans ("en":_) (HTTPStatus 409 _) = "Request is based on outdated state!"
trans ("en":_) (HTTPStatus 410 _) = "Page deleted! Try the <a href='/'>homepage</a>."
trans ("en":_) (HTTPStatus 411 _) = "I sent a bad request, according to this site." ++
    "<em>(Missing <code>Content-Length</code> header)</em>"
trans ("en":_) (HTTPStatus 412 _) = "Webpage doesn't meet our preconditions."
trans ("en":_) (HTTPStatus 413 _) = "Payload too large, please upload a smaller file!"
trans ("en":_) (HTTPStatus 414 _) = "Web address is too long for the site!"
trans ("en":_) (HTTPStatus 415 _) = "No representation available for supported filetypes!"
trans ("en":_) (HTTPStatus 416 _) = "Invalid byte-range of requested resource!"
trans ("en":_) (HTTPStatus 417 _) = "Site cannot satisfy our stated expectations!"
trans ("en":_) (HTTPStatus 418 _) = unlines [
    "<p>I'm a little teapot<br/>",
    "Short and stout<br/>",
    "Here is my handle<br/>",
    "And here is my spout.</p>>",
    "<p>When I get all steamed up<br/>",
    "Hear me shout<br/>",
    "<q>Tip me over<br/>",
    "And pour me out!</q></p>"
  ]
trans ("en":_) (HTTPStatus 421 _) = "Contacted wrong server!"
trans ("en":_) (HTTPStatus 422 _) = "Invalid <strong>WebDAV</strong> request!"
trans ("en":_) (HTTPStatus 423 _) = "<strong>WebDAV</strong> resource is locked!"
trans ("en":_) (HTTPStatus 424 _) = "Failed due to previous failure!"
trans ("en":_) (HTTPStatus 425 _) = "Site requires stronger security on our request!"
trans ("en":_) (HTTPStatus 426 _) = "Site requires newer networking-protocols!"
trans ("en":_) (HTTPStatus 428 _) = "Site requires additional protection to avoid loosing changes!"
trans ("en":_) (HTTPStatus 429 _) = "We sent this site too many requests for it to cope with!"
trans ("en":_) (HTTPStatus 431 _) = "I sent more auxiliary data than this site can cope with!"
trans ("en":_) (HTTPStatus 451 _) = "Requested page cannot legally be provided!"

trans ("en":_) (HTTPStatus 500 _) = "The site experienced an error generating this webpage. <em>The webmasters have probably already been automatically notified.</em>"
trans ("en":_) (HTTPStatus 501 _) = "Bad webform for this destination webaddress! " ++ 
    "<em>Method not implemented</em>."
trans ("en":_) (HTTPStatus 502 _) = "Proxyserver got a malformed response!"
trans ("en":_) (HTTPStatus 503 _) = "The site is not available right now!"
trans ("en":_) (HTTPStatus 504 _) = "The site took too long to respond! <em>(Behind proxy)</em>"
trans ("en":_) (HTTPStatus 505 _) = "The site does not speak the language as me! " ++
    "<em>(Unsupported HTTP version)</em>"
trans ("en":_) (HTTPStatus 506 _) = "The site is misconfigured!"
trans ("en":_) (HTTPStatus 507 _) = "Insufficient <strong>WebDAV</strong> storage!"
trans ("en":_) (HTTPStatus 508 _) = "<strong>WebDAV</strong> loop detected!"
trans ("en":_) (HTTPStatus 510 _) = "Further request extensions required!"
trans ("en":_) (HTTPStatus 511 _) = "Authentication into network required!"
trans ("en":_) (HTTPStatus _ error) = error -- TODO localize
--- END LOCALIZATION

trans (_:locales) err = trans locales err
trans [] err = trans ["en"] err

data Errors = UnsupportedScheme String | UnsupportedMIME String | RequiresInstall String String
    | OpenedWith String | ReadFailed String | RawXML String | MalformedResponse
    | ExcessiveRedirects | HTTPStatus Int String | GeminiError Char Char String
#if WITH_HTTP_URI
    | Http HttpException
#endif