我想在我的Play scala Web应用程序中进行错误处理.
我的应用程序与数据库进行对话以获取一些行,它遵循以下流程.
下面是我的伪代码.
def getResponse(name: String)
(implicit ctxt: ExecutionContext): Future[Response] = {
for {
future1 <- callFuture1(name)
future2 <- callFuture2(future1.data)
future3 <- callFuture3(future1.data, future2.data)
} yield future3
}
Run Code Online (Sandbox Code Playgroud)
上面的理解中的每个方法都返回一个未来,这些方法的签名如下.
private def callFuture1(name: String)
(implicit ctxt: ExecutionContext): Future[SomeType1] {...}
private def callFuture2(keywords: List[String])
(implicit ctxt: ExecutionContext): Future[SomeType2] {...}
private def callFuture3(data: List[SomeType3], counts: List[Int])
(implicit ctxt: ExecutionContext): Future[Response] {...}
Run Code Online (Sandbox Code Playgroud)
在下列情况下,如何处理错误/故障处理
- 编辑 -
我试图从getResponse()方法返回一个适当的错误响应,当callFuture中的任何一个失败并且不进入后续的futureCalls时.
我根据Peter Neyens的回答尝试了以下内容,但是给了我一个运行时错误..
def getResponse(name: String)
(implicit ctxt: ExecutionContext): Future[Response] = { …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Python Flask构建y第一个web服务.每次运行我的应用程序时,我无法理解Flask使用重新加载器发出重启的意思.
这是我的代码.
#!venv/bin/python
from flask import Flask
from flask import request
def buildCache():
print 'Hello World'
buildCache()
app = Flask(__name__)
@app.route('/search')
def index():
query = request.args.get('query','', type=str);
return query
if __name__ == '__main__':
app.run(debug = True)
Run Code Online (Sandbox Code Playgroud)
当我运行它
venv/bin/python ./app.py
Hello World
* Running on http://127.0.0.1:5000/
* Restarting with reloader
Hello World
Run Code Online (Sandbox Code Playgroud)
我不明白为什么buildCache方法被调用两次?它似乎与"使用重新加载器重新启动"有关,这是什么意思?我如何确保在服务器启动之前只执行一次buildCache.
我正在尝试使用Play 2.5依赖注入.我有以下类调用REST api并解析响应
class Client @Inject()(ws:WSClient, baseUrl: string) {
def this(ws:WSClient) = this(ws, "<url string>")
def getResponse() = {....}
....
}
Run Code Online (Sandbox Code Playgroud)
代码的调用者如下所示
var client = new Client(WS.client)
client.getResponse()
Run Code Online (Sandbox Code Playgroud)
我收到了警告.
不推荐使用包ws中的对象WS:将WSClient注入到组件中
我知道我需要注入WS.Client而不是将它显式传递给Client构造函数.但是我该怎么做?
===更新===
我不想从Controller注入Client或WSClient.我的控制器在运行时创建对象和类,我希望这些对象创建客户端对象.当我明确地将WS.client对象传递给Client对象时,我得到上述警告.
===更新2 ===
我的应用程序中有一个插件架构.当控制器启动一个动作时.它不知道它将执行哪些插件集.有些插件不需要WSClient,其中一些插件也是如此.所以我不想将WSClient的注入耦合到我的控制器中.每个插件都独立决定是否要调用远程服务.当插件决定调用远程服务时,它应该能够在它想要调用的客户端中注入WSClient.
控制器操作 - >确定要执行的插件 - >执行插件--->插件1(需要调用远程api,创建一个客户端对象,根据新客户端(WS.Client)).这是注射应该发生的地方,而不是控制器.
我的游戏项目中有第三方依赖.该第三方库对slf4j实现具有最终依赖性(非直接).
我收到了slf4j的重复绑定错误.
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:~/.ivy2/cache/ch.qos.logback/logback-classic/jars/logback-classic-1.1.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/~/.ivy2/cache/com.orgname.platform/platform-logging-client/jars/platform-logging-client-2.5.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]
Run Code Online (Sandbox Code Playgroud)
我尝试了一些事情,但可以摆脱错误.
"com.orgname.platform" % "platform-metric-client" % "1.0.4" excludeAll(
ExclusionRule(organization = "org.slf4j"))
Run Code Online (Sandbox Code Playgroud)
我也试过以下排除
"com.orgname.platform" % "platform-metric-client" % "1.0.4" exclude("org.slf4j","slf4j-jdk14)
Run Code Online (Sandbox Code Playgroud)
还有这一个
"com.orgname.platform" % "platform-metric-client" % "1.0.4" exclude("org.slf4j","slf4j-log4j12)
Run Code Online (Sandbox Code Playgroud)
由于我无法从第三方依赖项中删除slf4j,所以厌倦了删除对slf4j的播放依赖,通过修改projcts/plugin.sbt
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.4.6" exclude("org.slf4j", "slf4j-simple"))
Run Code Online (Sandbox Code Playgroud)
我该怎么做才能摆脱这个警告.这个警告如何影响日志记录?Scala实现将使用哪种日志记录实现?
我试图在我的python项目中使用级别db.我把注意力集中在python绑定PlyVel http://plyvel.readthedocs.org/en/latest/installation.html上,这似乎更好地维护并记录了python绑定.
但是plyvel的安装失败了
plyvel/_plyvel.cpp:359:10:致命错误:找不到'leveldb/db.h'文件
#include"leveldb/db.h"
所以我相信我必须在我的机器上安装leveldb.我没有为macosx找到leveldb的安装指南.我已经下载了leveldb的tarball,https://code.google.com/p/leveldb/downloads/list.Make文件编译代码,但plylevel仍然失败.我应该如何编译级别数据库,使其二进制文件可用于plyvel.
我有以下类层次结构.
trait Item {val id: String}
case class MItem(override val id: String, val name: String) extends Item
class DItem(override val id: String, override val name: String, val name2: String) extends MItem(id, name)
val d = new DItem("1", "one", "another one")
println(d)
Run Code Online (Sandbox Code Playgroud)
预期产出
DItem(1, one, another one)
Run Code Online (Sandbox Code Playgroud)
实际产出
Mitem(1,one)
Run Code Online (Sandbox Code Playgroud)
为什么会这样呢?建议什么,以便我得到我的对象的真实类型和非类型的超类.
在我的播放应用程序中,我打算模拟一个案例类.我能够这样做,但它创建了一个所有成员变量为null的对象.有没有办法创建案例类的模拟对象,以便对象可以初始化一些成员?
case class User(name: String, address: String)
val mockUser = mock[User]
user.name // null
user.address //null
Run Code Online (Sandbox Code Playgroud)
我如何创建一个mockUser,以便我可以为名称和地址分配一些值?
编辑:
我需要能够模拟对象,因为我希望有一个成员方法的预定义行为.(这个成员方法调用外部服务,我不想在进行单元测试时调用外部服务.)成员方法在另一个成员方法中调用,我想测试.
我试图用于理解来执行系列期货列表.一个未来的输出成为下一个未来的输入.下面是示例代码.
for {
x <- plugins(0).execute(input)
y <- plugins(1).execute(x)
z <- plugins(2).excute(y)
} yield z
Run Code Online (Sandbox Code Playgroud)
上面的代码更像是一个瀑布,每个插件(i)在一些输入上执行,并将输出传递给下一个插件.最后一个插件的输出是最终输出.plugins是一个Seq of Plugin对象.execute方法返回Future.我想使上面的代码更通用,其中插件中的插件数量是动态的.我该如何实现它.