diff options
author | Randall Leeds <randall@apache.org> | 2012-03-18 20:19:27 -0700 |
---|---|---|
committer | Randall Leeds <randall@apache.org> | 2012-10-25 09:41:08 -0700 |
commit | 4b6475da2cd9ff392736e52a6b834e7827fe3ee3 (patch) | |
tree | 1daeec99f9926d92c999361a1dd291fc04b58237 | |
parent | b99ec792110d0f06a42e38fb6f0208d3b6b3bcc7 (diff) | |
download | couchdb-4b6475da2cd9ff392736e52a6b834e7827fe3ee3.tar.gz |
improve file I/O error logging and handling
It's better to let these errors bubble and/or not give them special
treatment when file:format_error/1 can do a better job of describing
the failure.
This is a forward-port of work done on 1.2.x, encompassing the
following commits:
04c4a1041de06e8e39983cf9616c8baf9d889106
ede9482fc3c9e629572ab1376d88f4499c4c8beb
cd238b42d1333cece5ab899c35c3b83ada8d448a
af9e1c883ca4accb209cccc7ac9d26efa4daf1fd
ba8fa86890087650a02a8ebaaea2537468999472
-rw-r--r-- | src/couchdb/couch_config.erl | 11 | ||||
-rw-r--r-- | src/couchdb/couch_config_writer.erl | 8 | ||||
-rw-r--r-- | src/couchdb/couch_file.erl | 14 | ||||
-rw-r--r-- | src/couchdb/couch_log.erl | 8 | ||||
-rw-r--r-- | src/couchdb/couch_server.erl | 2 | ||||
-rw-r--r-- | src/couchdb/couch_server_sup.erl | 14 |
6 files changed, 30 insertions, 27 deletions
diff --git a/src/couchdb/couch_config.erl b/src/couchdb/couch_config.erl index 79effda0b..96fabbad8 100644 --- a/src/couchdb/couch_config.erl +++ b/src/couchdb/couch_config.erl @@ -187,13 +187,10 @@ parse_ini_file(IniFile) -> case file:read_file(IniFilename) of {ok, IniBin0} -> IniBin0; - {error, eacces} -> - throw({file_permission_error, IniFile}); - {error, enoent} -> - Fmt = "Couldn't find server configuration file ~s.", - Msg = ?l2b(io_lib:format(Fmt, [IniFilename])), - ?LOG_ERROR("~s~n", [Msg]), - throw({startup_error, Msg}) + {error, Reason} = Error -> + ?LOG_ERROR("Could not read server configuration file ~s: ~s", + [IniFilename, file:format_error(Reason)]), + throw(Error) end, Lines = re:split(IniBin, "\r\n|\n|\r|\032", [{return, list}]), diff --git a/src/couchdb/couch_config_writer.erl b/src/couchdb/couch_config_writer.erl index decd269a7..21f1c3f9d 100644 --- a/src/couchdb/couch_config_writer.erl +++ b/src/couchdb/couch_config_writer.erl @@ -22,6 +22,8 @@ -export([save_to_file/2]). +-include("couch_db.hrl"). + %% @spec save_to_file( %% Config::{{Section::string(), Option::string()}, Value::string()}, %% File::filename()) -> ok @@ -38,9 +40,9 @@ save_to_file({{Section, Key}, Value}, File) -> case file:write_file(File, NewFileContents) of ok -> ok; - {error, eacces} -> - {file_permission_error, File}; - Error -> + {error, Reason} = Error -> + ?LOG_ERROR("Could not write config file ~s: ~s", + [File, file:format_error(Reason)]), Error end. diff --git a/src/couchdb/couch_file.erl b/src/couchdb/couch_file.erl index 2c2f11ac3..e00b0f002 100644 --- a/src/couchdb/couch_file.erl +++ b/src/couchdb/couch_file.erl @@ -53,17 +53,19 @@ open(Filepath, Options) -> ignore -> % get the error receive - {Ref, Pid, Error} -> + {Ref, Pid, {error, Reason} = Error} -> case process_info(self(), trap_exit) of {trap_exit, true} -> receive {'EXIT', Pid, _} -> ok end; {trap_exit, false} -> ok end, - case Error of - {error, eacces} -> {file_permission_error, Filepath}; - _ -> Error - end + ?LOG_DEBUG("Could not open file ~s: ~s", + [Filepath, file:format_error(Reason)]), + Error end; Error -> + % We can't say much here, because it could be any kind of error. + % Just let it bubble and an encapsulating subcomponent can perhaps + % be more informative. It will likely appear in the SASL log, anyway. Error end. @@ -290,7 +292,7 @@ init({Filepath, Options, ReturnPid, Ref}) -> {ok, #file{fd=Fd}}; false -> ok = file:close(Fd), - init_status_error(ReturnPid, Ref, file_exists) + init_status_error(ReturnPid, Ref, {error, eexist}) end; false -> maybe_track_open_os_files(Options), diff --git a/src/couchdb/couch_log.erl b/src/couchdb/couch_log.erl index a38c38aed..fc7b39364 100644 --- a/src/couchdb/couch_log.erl +++ b/src/couchdb/couch_log.erl @@ -89,10 +89,10 @@ init([]) -> case file:open(Filename, [append]) of {ok, Fd} -> {ok, #state{fd = Fd, level = Level, sasl = Sasl}}; - {error, eacces} -> - {stop, {file_permission_error, Filename}}; - Error -> - {stop, Error} + {error, Reason} -> + ReasonStr = file:format_error(Reason), + io:format("Error opening log file ~s: ~s", [Filename, ReasonStr]), + {stop, {error, ReasonStr, Filename}} end. debug_on() -> diff --git a/src/couchdb/couch_server.erl b/src/couchdb/couch_server.erl index 0f40c51d7..694daee43 100644 --- a/src/couchdb/couch_server.erl +++ b/src/couchdb/couch_server.erl @@ -319,6 +319,8 @@ handle_call({open_result, DbName, {ok, OpenedDbPid}, Options}, _From, Server) -> ok end, {reply, ok, Server}; +handle_call({open_result, DbName, {error, eexist}, Options}, From, Server) -> + handle_call({open_result, DbName, file_exists, Options}, From, Server); handle_call({open_result, DbName, Error, Options}, _From, Server) -> [{DbName, {opening,Opener,Froms}}] = ets:lookup(couch_dbs_by_name, DbName), lists:foreach(fun(From) -> diff --git a/src/couchdb/couch_server_sup.erl b/src/couchdb/couch_server_sup.erl index 7baede338..be3c3a3e4 100644 --- a/src/couchdb/couch_server_sup.erl +++ b/src/couchdb/couch_server_sup.erl @@ -46,7 +46,9 @@ start_server(IniFiles) -> {ok, [PidFile]} -> case file:write_file(PidFile, os:getpid()) of ok -> ok; - Error -> io:format("Failed to write PID file ~s, error: ~p", [PidFile, Error]) + {error, Reason} -> + io:format("Failed to write PID file ~s: ~s", + [PidFile, file:format_error(Reason)]) end; _ -> ok end, @@ -121,12 +123,10 @@ start_server(IniFiles) -> end end || Uri <- Uris], case file:write_file(UriFile, Lines) of ok -> ok; - {error, eacces} -> - ?LOG_ERROR("Permission error when writing to URI file ~s", [UriFile]), - throw({file_permission_error, UriFile}); - Error2 -> - ?LOG_ERROR("Failed to write to URI file ~s: ~p~n", [UriFile, Error2]), - throw(Error2) + {error, Reason2} = Error -> + ?LOG_ERROR("Failed to write to URI file ~s: ~s", + [UriFile, file:format_error(Reason2)]), + throw(Error) end end, |