1/*
2 * util_error.c : serf utility routines for wrapping serf status codes
3 *
4 * ====================================================================
5 *    Licensed to the Apache Software Foundation (ASF) under one
6 *    or more contributor license agreements.  See the NOTICE file
7 *    distributed with this work for additional information
8 *    regarding copyright ownership.  The ASF licenses this file
9 *    to you under the Apache License, Version 2.0 (the
10 *    "License"); you may not use this file except in compliance
11 *    with the License.  You may obtain a copy of the License at
12 *
13 *      http://www.apache.org/licenses/LICENSE-2.0
14 *
15 *    Unless required by applicable law or agreed to in writing,
16 *    software distributed under the License is distributed on an
17 *    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18 *    KIND, either express or implied.  See the License for the
19 *    specific language governing permissions and limitations
20 *    under the License.
21 * ====================================================================
22 */
23#include <serf.h>
24
25#include "svn_utf.h"
26#include "private/svn_error_private.h"
27
28#include "ra_serf.h"
29
30/*
31 * Undefine the helpers for creating errors.
32 *
33 * *NOTE*: Any use of these functions in any other function may need
34 * to call svn_error__locate() because the macro that would otherwise
35 * do this is being undefined and the filename and line number will
36 * not be properly set in the static error_file and error_line
37 * variables.
38 */
39#undef svn_error_create
40#undef svn_error_createf
41#undef svn_error_quick_wrap
42#undef svn_error_wrap_apr
43#undef svn_ra_serf__wrap_err
44
45svn_error_t *
46svn_ra_serf__wrap_err(apr_status_t status,
47                      const char *fmt,
48                      ...)
49{
50  const char *serf_err_msg = serf_error_string(status);
51  svn_error_t *err;
52  va_list ap;
53
54  err = svn_error_create(status, NULL, NULL);
55
56  if (serf_err_msg || fmt)
57    {
58      const char *msg;
59      const char *err_msg;
60      char errbuf[255]; /* Buffer for APR error message. */
61
62      if (serf_err_msg)
63        {
64          err_msg = serf_err_msg;
65        }
66      else
67        {
68          svn_error_t *utf8_err;
69
70          /* Grab the APR error message. */
71          apr_strerror(status, errbuf, sizeof(errbuf));
72          utf8_err = svn_utf_cstring_to_utf8(&err_msg, errbuf, err->pool);
73          if (utf8_err)
74            err_msg = NULL;
75          svn_error_clear(utf8_err);
76        }
77
78      /* Append it to the formatted message. */
79      if (fmt)
80        {
81          va_start(ap, fmt);
82          msg = apr_pvsprintf(err->pool, fmt, ap);
83          va_end(ap);
84        }
85      else
86        {
87          msg = "ra_serf";
88        }
89      if (err_msg)
90        {
91          err->message = apr_pstrcat(err->pool, msg, ": ", err_msg,
92                                     SVN_VA_NULL);
93        }
94      else
95        {
96          err->message = msg;
97        }
98    }
99
100  return err;
101}
102