Erlang中的错误报告

Erlang中的错误报告,erlang,try-catch,cowboy,Erlang,Try Catch,Cowboy,我已经采取了牛仔的例子代码和布鲁克恩它 默认请求处理程序的代码如下所示: -module(default_handler). -behaviour(cowboy_http_handler). -export([init/3, handle/2, terminate/2]). init({_Any, http}, Req, []) -> {ok, Req, undefined}. handle(Req, State) -> {ok, Req2} = cowboy_h

我已经采取了牛仔的例子代码和布鲁克恩它

默认请求处理程序的代码如下所示:

-module(default_handler).
-behaviour(cowboy_http_handler).
-export([init/3, handle/2, terminate/2]).

init({_Any, http}, Req, []) ->
    {ok, Req, undefined}.

handle(Req, State) ->
    {ok, Req2} = cowboy_http_req:reply(200, [], <<"Hello world!">>, Req),
    {ok, Req2, State}.

terminate(_Req, _State) ->
    ok.
-模块(默认\u处理程序)。
-行为(牛仔处理者)。
-导出([init/3,handle/2,terminate/2])。
init({u Any,http},Req,[])->
{确定,请求,未定义}。
句柄(请求、状态)->
{ok,Req2}=cowboy_http_req:reply(200,[],req),
{好的,请求2,状态}。
终止(_请求,_状态)->
好啊
这很简单,但我想制作返回文件,所以我将其更改为:

-module(default_handler).
-behaviour(cowboy_http_handler).
-export([init/3, handle/2, terminate/2]).

init({_Any, http}, Req, []) ->
    {ok, Req, undefined}.

handle(Req, State) ->
    try 
    {Path, Req1} = cowboy_http_req:path(Req),
    {ok, File} = file:read_file(Path),
    cowboy_http_req:reply(200, [], File, Req1)
    of 
    {ok, Req2} ->
        {ok, Req2, State}
    catch
    _ -> 
        {ok, Req3} = cowboy_http_req:reply(200, [], <<"Hello world!">>, Req),
        {ok, Req3, State}
    end.

terminate(_Req, _State) ->
    ok.
-模块(默认\u处理程序)。
-行为(牛仔处理者)。
-导出([init/3,handle/2,terminate/2])。
init({u Any,http},Req,[])->
{确定,请求,未定义}。
句柄(请求、状态)->
尝试
{Path,Req1}=cowboy_http_req:Path(req),
{ok,File}=File:read_File(路径),
牛仔http需求:回复(200,[],文件,需求1)
属于
{好的,请求2}->
{好的,请求2,状态}
抓住
_ -> 
{ok,Req3}=cowboy_http_req:reply(200,[],req),
{好的,请求3,状态}
结束。
终止(_请求,_状态)->
好啊
try-catch应该处理这样一个事实:可能没有文件,但它没有。为什么呢

当我试图获取一个不在那里的文件时,控制台中会出现一个很大的错误报告,有人能告诉我为什么吗

=ERROR REPORT==== 15-Jun-2012::14:24:54 ===
** Handler default_handler terminating in handle/2
   for the reason error:{badmatch,{error,badarg}}
** Options were []
** Handler state was undefined
** Request was [{socket,#Port<0.1515>},
                {transport,cowboy_tcp_transport},
                {connection,keepalive},
                {pid,<0.1175.0>},
                {method,'GET'},
                {version,{1,1}},
                {peer,undefined},
                {host,[<<"localhost">>]},
                {host_info,undefined},
                {raw_host,<<"localhost">>},
                {port,8080},
                {path,[<<"favicon.ico">>]},
                {path_info,undefined},
                {raw_path,<<"/favicon.ico">>},
                {qs_vals,undefined},
                {raw_qs,<<>>},
                {bindings,[]},
                {headers,
                    [{'Accept-Charset',<<"ISO-8859-1,utf-8;q=0.7,*;q=0.3">>},
                     {'Accept-Language',<<"en-US,en;q=0.8">>},
                     {'Accept-Encoding',<<"gzip,deflate,sdch">>},
                     {'User-Agent',
                         <<"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.19 (KHTML, like Gecko) Ubuntu/10.10 Chromium/18.0.1025.151 Chrome/18.0.1025.151 Safari/535.19">>},
                     {'Accept',<<"*/*">>},
                     {'Connection',<<"keep-alive">>},
                     {'Host',<<"localhost">>}]},
                {p_headers,[{'Connection',[<<"keep-alive">>]}]},
                {cookies,undefined},
                {meta,[]},
                {body_state,waiting},
                {buffer,<<>>},
                {resp_state,waiting},
                {resp_headers,[]},
                {resp_body,<<>>},
                {onresponse,undefined},
                {urldecode,{#Fun<cowboy_http.urldecode.2>,crash}}]
** Stacktrace: [{default_handler,handle,2,
                                 [{file,"src/default_handler.erl"},{line,13}]},
                {cowboy_http_protocol,handler_handle,3,
                                      [{file,"src/cowboy_http_protocol.erl"},
                                       {line,298}]}]
=错误报告===2012年6月15日::14:24:54===
**处理程序默认值\u处理程序终止于句柄/2
原因错误:{badmatch,{error,badarg}}
**选项为[]
**处理程序状态未定义
**请求是[{socket,#Port},
{运输,牛仔运输},
{connection,keepalive},
{pid,},
{method,'GET'},
{version,{1,1}},
{对等,未定义},
{host,[]},
{host_info,未定义},
{raw_host,},
{port,8080},
{path,[]},
{path_info,未定义},
{raw_path,},
{qs_vals,未定义},
{raw_qs,},
{绑定,[]},
{标题,
[{'Accept-Charset',},
{'Accept-Language',},
{'Accept-Encoding',},
{'User-Agent',
},
{'Accept',},
{'Connection',},
{'Host',}]},
{p_头,[{'Connection',[]}},
{cookies,未定义},
{meta,[]},
{身体状态,等待},
{buffer,},
{resp_state,waiting},
{resp_头,[]},
{resp_body,},
{onresponse,未定义},
{urldecode,{有趣,崩溃}]
**Stacktrace:[{default_处理程序,句柄,2,
[{file,“src/default_handler.erl”},{line,13}]},
{cowboy_http_协议,handler_handle,3,
[{文件,“src/cowboy_http_protocol.erl”},
{line,298}]}]

可能是因为它如何计算catch子句,请参见

如果在EXPR求值期间发生异常,但没有正确类的匹配ExceptionPattern与真正的保护序列,则会传递异常,就像EXPR未包含在try表达式中一样

如果不查找默认值(即throw),则需要指定错误(error、throw或exit)

try Exprs of
   Pattern1 [when GuardSeq1] ->
       Body1;
    ...;
   PatternN [when GuardSeqN] ->
        BodyN
catch
    [Class1:]ExceptionPattern1 [when ExceptionGuardSeq1] ->
        ExceptionBody1;
    ...;
   [ClassN:]ExceptionPatternN [when ExceptionGuardSeqN] ->
        ExceptionBodyN
捕获所有错误将被写为

catch
    _:_ -> 
请注意,您是如何将class和ExpressionPattern都指定为“不在乎”的


希望这能有所帮助,因为到目前为止我只“涉猎”过二郎

可能是因为它如何计算catch子句,请参见

如果在EXPR求值期间发生异常,但没有正确类的匹配ExceptionPattern与真正的保护序列,则会传递异常,就像EXPR未包含在try表达式中一样

如果不查找默认值(即throw),则需要指定错误(error、throw或exit)

try Exprs of
   Pattern1 [when GuardSeq1] ->
       Body1;
    ...;
   PatternN [when GuardSeqN] ->
        BodyN
catch
    [Class1:]ExceptionPattern1 [when ExceptionGuardSeq1] ->
        ExceptionBody1;
    ...;
   [ClassN:]ExceptionPatternN [when ExceptionGuardSeqN] ->
        ExceptionBodyN
捕获所有错误将被写为

catch
    _:_ -> 
请注意,您是如何将class和ExpressionPattern都指定为“不在乎”的

希望这能有所帮助,因为到目前为止我只“涉猎”过二郎

这一行:

{Path, Req1} = cowboy_http_req:path(Req),
实际上返回一个二进制文件列表,比如[,],而不是像“/path/path2”这样的东西,这应该是您实际需要的

因此,要形成文件系统路径:

{Path, Req1} = cowboy_http_req:path(Req),
FsPath = lists:foldl(
    fun(PathComponent, Acc) ->
        string:join([Acc, erlang:binary_to_list(PathComponent)], "/")
    end,
    "",
    Path
),
{ok, File} = file:read_file(FsPath),
(您得到的badarg错误是因为file:read_file/1的参数不是字符串(列表),而是二进制文件列表,这不是预期的参数

而catch需要一个:\u子句,就像Harald answer所说的那样

干杯。

这句话:

{Path, Req1} = cowboy_http_req:path(Req),
实际上返回一个二进制文件列表,比如[,],而不是像“/path/path2”这样的东西,这应该是您实际需要的

因此,要形成文件系统路径:

{Path, Req1} = cowboy_http_req:path(Req),
FsPath = lists:foldl(
    fun(PathComponent, Acc) ->
        string:join([Acc, erlang:binary_to_list(PathComponent)], "/")
    end,
    "",
    Path
),
{ok, File} = file:read_file(FsPath),
(您得到的badarg错误是因为file:read_file/1的参数不是字符串(列表),而是二进制文件列表,这不是预期的参数

而catch需要一个:\u子句,就像Harald answer所说的那样


干杯。

在这种情况下,[]不意味着类对象是可选的吗?是的,但如果省略类erlang,则认为它是可选的。[]不意味着类对象在这种情况下是可选的吗?是的,但如果省略类erlang,则认为它不是可选的:对象?难道:对象不是可选的吗?