include <limits> for std::numeric_limits
[chromium-blink-merge.git] / net / base / net_errors.cc
blob7c219afbf9015b47497ca693ad6e617f94634d7a
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/base/net_errors.h"
7 #include "base/basictypes.h"
8 #include "base/metrics/histogram.h"
9 #include "base/strings/stringize_macros.h"
11 namespace {
13 // Get all valid error codes into an array as positive numbers, for use in the
14 // |GetAllErrorCodesForUma| function below.
15 #define NET_ERROR(label, value) -(value),
16 const int kAllErrorCodes[] = {
17 #include "net/base/net_error_list.h"
19 #undef NET_ERROR
21 } // namespace
23 namespace net {
25 const char kErrorDomain[] = "net";
27 std::string ErrorToString(int error) {
28 return "net::" + ErrorToShortString(error);
31 std::string ErrorToShortString(int error) {
32 if (error == 0)
33 return "OK";
35 const char* error_string;
36 switch (error) {
37 #define NET_ERROR(label, value) \
38 case ERR_ ## label: \
39 error_string = # label; \
40 break;
41 #include "net/base/net_error_list.h"
42 #undef NET_ERROR
43 default:
44 NOTREACHED();
45 error_string = "<unknown>";
47 return std::string("ERR_") + error_string;
50 bool IsCertificateError(int error) {
51 // Certificate errors are negative integers from net::ERR_CERT_BEGIN
52 // (inclusive) to net::ERR_CERT_END (exclusive) in *decreasing* order.
53 // ERR_SSL_PINNED_KEY_NOT_IN_CERT_CHAIN is currently an exception to this
54 // rule.
55 return (error <= ERR_CERT_BEGIN && error > ERR_CERT_END) ||
56 (error == ERR_SSL_PINNED_KEY_NOT_IN_CERT_CHAIN);
59 std::vector<int> GetAllErrorCodesForUma() {
60 return base::CustomHistogram::ArrayToCustomRanges(
61 kAllErrorCodes, arraysize(kAllErrorCodes));
64 Error FileErrorToNetError(base::File::Error file_error) {
65 switch (file_error) {
66 case base::File::FILE_OK:
67 return net::OK;
68 case base::File::FILE_ERROR_ACCESS_DENIED:
69 return net::ERR_ACCESS_DENIED;
70 case base::File::FILE_ERROR_INVALID_URL:
71 return net::ERR_INVALID_URL;
72 case base::File::FILE_ERROR_NOT_FOUND:
73 return net::ERR_FILE_NOT_FOUND;
74 default:
75 return net::ERR_FAILED;
79 } // namespace net