小编Sch*_*ute的帖子

PHP - 无法捕获Google API lib抛出的异常

我想捕获Google API PHP库引发的异常,但由于某种原因,它会在到达catch块之前生成" 致命错误:未捕获的异常 ".

在我的应用程序中,我有这样的事情:

try {
    $google_client->authenticate($auth_code);
} catch (Exception $e) {
    // do something
}
Run Code Online (Sandbox Code Playgroud)

这是Google_Client的authenticate():

public function authenticate($code)
{
    $this->authenticated = true;
    return $this->getAuth()->authenticate($code);
}
Run Code Online (Sandbox Code Playgroud)

authenticate($code)上面是Google_Auth_OAuth2 :: authenticate()的,其中在某一点抛出异常:

throw new Google_Auth_Exception(
    sprintf(
        "Error fetching OAuth2 access token, message: '%s'",
        $decodedResponse
    ),
    $response->getResponseHttpCode()
);
Run Code Online (Sandbox Code Playgroud)

如果我在Google_Client的身份验证中放置一个try/catch块,它会捕获异常,但如果没有它,程序就会死掉而不是从我的应用程序到达主try/catch块.

据我所知,这不应该发生.有任何想法吗?

php exception-handling google-api-php-client

8
推荐指数
1
解决办法
4982
查看次数

脚手架测试失败,使用ActionController :: InvalidAuthenticityToken

在Rails 4.1.6中,我InvalidAuthenticityToken在运行使用scaffold生成的测试时遇到错误.有没有办法禁用特定测试的真实性令牌检查?

rails g scaffold user name:string email:string password_digest:string
rake
Run Code Online (Sandbox Code Playgroud)

输出:

...

  1) Error:
UsersControllerTest#test_should_create_user:
ActionController::InvalidAuthenticityToken: ActionController::InvalidAuthenticityToken
    test/controllers/users_controller_test.rb:21:in `block (2 levels) in <class:UsersControllerTest>'
    test/controllers/users_controller_test.rb:20:in `block in <class:UsersControllerTest>'

...
Run Code Online (Sandbox Code Playgroud)

这是源代码:

test "should create admin_user" do
  assert_difference('Admin::User.count') do
    post :create, admin_user: { email: @admin_user.email, password_digest: @admin_user.password_digest, username: @admin_user.username }
  end

  assert_redirected_to admin_user_path(assigns(:admin_user))
end
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails ruby-on-rails-4

6
推荐指数
1
解决办法
850
查看次数

检查默认响应时避免代码重复

我有一个调用外部 API 的 Java 程序(RealApi在下面的代码中),有时我想避免调用这个 API,而是返回预先构造的响应(由 生成FakeApi)。

所以,我最终在我的大多数方法中复制了这种结构:

public Type1 m1(String s) {
    try {
        Type1 r = FakeApi.m1(s);
        if (r != null) {
            return r;
        }
    } catch (Exception e) {
        // log error
    }

    return RealApi.m1(s);
}
Run Code Online (Sandbox Code Playgroud)

有哪些选项可以避免在任何地方复制这个 try/catch 块?重要的是,如果FakeApi抛出异常或返回 null,则RealApi必须调用 。

java code-duplication

2
推荐指数
1
解决办法
53
查看次数