在具有指定类型的函数中输入透析器中的错误

时间:2015-06-07 02:07:20

标签: erlang dialyzer

分析以下功能时,我在透析器中收到错误。

-spec do_request(Method, Type, Url, Expect, Headers, Body, Client) -> Response::client_response() 
when 
    Method  :: method(),
    Type    :: content_type(),
    Url     :: url(),
    Expect  :: status_codes(),
    Headers :: headers(),
    Body    :: body(),
    Client  :: #client{}.

do_request(Method, Type, Url, Expect, Headers, Body, Client) -> 
    Client2 = check_expired(Client),
    Headers2 = add_auth_header(Headers, Client2),
    %% error occurs on this line
    Response = restc:request(Method, Type, binary_to_list(Url), Expect, Headers2, Body),
    %%
    {Response, Client2}.

错误是:

The call restc:request(Method::any(),Type::any(),
[byte()],Expect::any(),Headers2::[{binary(),binary()},...],
Body::any()) breaks the contract (Method::method(), Type::content_type(),  
Url::url(), Expect::status_codes(), Headers::headers(), 
Body::body()) -> Response::response()

restc:request具有以下类型:

-spec request(Method::method(), Type::content_type(), Url::url(),
Expect::status_codes(), Headers::headers(), Body::body()) -> Response::response().

通话使用的类型是:

-type method()       :: head | get | put | post | trace | options | delete.
-type url()          :: binary().
-type headers()      :: [header()].
-type header()       :: {binary(), binary()}.
-type status_codes() :: [status_code()].
-type status_code()  :: integer().
-type reason()       :: term().
-type content_type() :: json | xml | percent.
-type property()     :: atom() | tuple().
-type proplist()     :: [property()].
-type body()         :: proplist().
-type response()     :: {ok, Status::status_code(), Headers::headers(), Body::body()} |
                        {error, Status::status_code(), Headers::headers(), Body::body()} |
                        {error, Reason::reason()}.
-type client_response()       :: {response(), #client{}}.
-type token_type()     :: bearer | unsupported.

为什么当我指定传递的变量类型时,透析器会说我的调用是以any()类型传递变量?我查看了调用链以验证类型规范是否一致(并且与其他模块一致)。

1 个答案:

答案 0 :(得分:2)

问题是url()被指定为binary()但你传入[byte()](字符串)。您需要将url()的类型规范改为iodata(),或者通过先将其转换为二进制来限制输入。