我有一个Erlang应用程序,它可以从文件中获取一些键/值对。
在其中一个文件中,我有一个到另一个文件夹的路径,而不是键/值对,其中有键/值对。
文件的读取工作正常,但每当我必须从文件中读取路径,然后再读取后续文件时,应用程序就会抱怨文件不存在。
如果我跳到容器中检查,我可以看到文件和后续文件。所以我猜我遗漏了一些东西。
这是我所拥有的:
get_kvs("keyvalue.properties"), %% <-- This works
{ok, PathToFile} = file:read_file("pathtofile.properties"),
get_kvs(PathToFile), %% <-- This crashes
档案:
keyvalue.properties
:
key_1=val_1
key_2=val_2
key_3=val_3
pathtofile.properties
:
/data/myfolder/hidden_keyvalue.properties
/data/myfolder/hidden_keyvalue.properties
:
extra_key1=extra_val1
extra_key2=extra_val2
extra_key3=extra_val3
而get_metadata
函数:
get_metadata(FileName) ->
io:format(FileName),
{ok, MetadataFile} = file:read_file(FileName),
io:format(MetadataFile),
Lines = binary:split(MetadataFile, <<"\n">>, [trim, global]),
make_tuples(Lines, []).
make_tuples([Line|Lines], Acc) ->
[Key, Value] = binary:split(Line, <<"=">>),
make_tuples(Lines, [{Key, Value}|Acc]);
make_tuples([], Acc) -> lists:reverse(Acc).
无论何时运行它,我都可以看到PathToFile
被正确填充,但当我试图读取路径时,我得到下面的错误:
keyvalue.propertiesextra_key_1=extra_val_1
extra_key_2=extra_val_2
extra_key_3=extra_val_3
/data/myfolder/hidden_keyvalue.properties
=CRASH REPORT==== 23-Mar-2022::07:46:30.612093 ===
crasher:
initial call: cowboy_stream_h:request_process/3
pid: <0.735.0>
registered_name: []
exception error: no match of right hand side value {error,enoent}
in function hello_handler:get_metadata/1 (/data/apps/hello_server/src/hello_handler.erl, line 40)
in call from hello_handler:child_call/1 (/data/apps/hello_server/src/hello_handler.erl, line 28)
你知道我错过了什么吗
1条答案
按热度按时间pbpqsu0x1#
After the OP has been modified to reflect the actual point of failure by removing
try-catch
, the error seems to be{error, enoent }
which meansThe file does not exist
. However, the same function is working in some scenarios and not when the path of the file to be read is itself taken from another file.Just make sure there are mo additional characters, like newlines or non-printable characters after the content of the file which should actually be a valid path.
For example, when I tried with a value as such,
<<"hidden_keyvalue.properties\n\n">>
, thenread_file
gave me same result as{error, enoent}
.So it could be possible that the content of the file from which paths are read has additional non-printable characters at the end.
Ignore (Wrong Assumption)
I tried with a local setup and I think this line inside
make_tuples
is causing that behavior.Given we are doing inside
get_metadata
From the provided text, it appears that the content of
keyvalue.proiperties
file is ...And with that last line,
make_tuples
while doing a match will fail the following way...That pattern-match expression requires exactly 2 elements on the right-hand side(the Term/Value side). Since the line with path-entry in the
keyvalue.properties
does not have an=
, so the split produces a list with 1 element, and so the match fails.To address this...
keyvalue.properties
so that every line is a validkey-value
pair, but not sure how feasible it will be in the context of the program.Assumptions
get_kvs
invokesget_metadata
which in turn invokesmake_tuples
.keyvalue.properties
file content has both valid key-value pairs and also some non-key-value entries