returning error message to front end Scala Play - scala

I need to send an error status to the front end using Scala play. Ok is to say all is ok but which one is for error? Can this be done from any Scala method I created?
thanks

It can be done from any method, you just need to use correspondent Result. For the general server error it is InternalServerErrorlike
def index = Action {
InternalServerError("Some error happens")
}
You can find more results here: https://playframework.com/documentation/2.6.7/api/scala/index.html#play.api.mvc.Results
The examples are:
InsufficientStorage //507
Locked //423
You can also send your own state as simple as
new Status(500) //aka InternalServerError

Related

How to provide an explicit error/failure message in the Scala fastparse library?

I'm using Li Haoyi's FastParse library. I have several situations where I'd like to provide explicit failure messages.
For example:
def courseRE[p: P]: P[Regex] =
P(CharIn("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.|*+[]()-^$").repX(1).!).map { re =>
try { re.r }
catch { case e => failure(s"Ill-formed regular expression: ${re}.") }
}
But there is (apparently) no failure function.
Any suggested work-arounds? Simply throwing an exception doesn't give any context information.
I haven't yet found a good solution. I don't like the solution proposed by #user2963757 because it loses all the information from the parser about what it was looking for, where it was looking, etc.
This is raised a number of times in the FastParse issues list on GitHub. See issue 213, issue 187, issue 243, and pull request 244. There are a few vague suggestions but as far as I can tell the pull request hasn't been acted on (as of 2023-02-09).
The best I've found so far is defining this in an accessible location:
// Fail with a message. See https://github.com/com-lihaoyi/fastparse/issues/213
// The message shows up as "Expected ..."; phrase it appropriately.
private def Fail[T](expected: String)(implicit ctx: P[_]): P[T] = {
val res = ctx.freshFailure()
if (ctx.verboseFailures) ctx.setMsg(ctx.index, () => expected)
res
}
To use it:
P(CharIn("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.|*+[]()-^$").repX(1).!).flatMap(re =>
Try(re.r)
.map(Pass(_))
.getOrElse(Fail("<Well-formed regular expression>"))
)
Trying to parse "^CS1[1345" yields
Expected <Well-formed regular expression>:1:10, found ""
Notice that the failure message has to be stated in terms of what was expected, not the actual problem. The actual error message thrown by the exception usually doesn't work well in this situation. I'm also not getting the fragment that it found.
Unfortunately, even this message is usually unavailable. For example, parsing a larger piece of my input results in
Expected (courseSpecDef | minUnits | xOf | courseSpec):1:14, found "^CS1[1345 "
I'd like to be able to surface the more exact error of "Unclosed character class" but seemingly can't.
By the way, I looked in the documentation, source code, and the sample parsers (PythonParse and ScalaParse) for examples of the use of the Fail parser. Can't find any. The only one is the one shown in the documentation, which doesn't compose with another parser.
If anyone has a better solution, I'd still love to hear it.

odoo12 how to show success message without break

everybody, please help
in odoo, i know that the error message showing was use raise UserError(), but this function will break the function and rollback in database, what i want to do is just simplely show the successful message to user without break, like operation was successful!.
of course, i have try to use the wizard, but the footer is not working, the page always show the save and discard
appreciate if anybody can help me on this.
thanks.
In that, you can use the onchange API to work around this,
#api.onchange('my_field')
def my_field_change(self):
// warning message
return {
'warning': {'title': _('Error'), 'message': _('Error message'),},
}
With that, Perform the change with your case return the warning message and it does not break the process.You can find a similar behavior on Odoo Sale App on sale order line product change operation.
Thanks

Calling GCP Translate API within Dataproc pyspark map

I am trying to call the language detection method of the translate client api from pyspark for each row in a file.
I created a map method as the following but the job seems to just freeze with no error. If I remove the call to the translate API it executes fine. Is it possible to call Google client API methods within pySpark map ?
mapping method to do translation
def doTranslate(data):
translate_client = translate.Client()
# Get the message information
messageId = data[0]
messageContent = data[6]
detectedLang = translate_client.detect_language(messageContent)
r = []
r.append(detectedLang)
return r
Figured it out!! your question led me in the right direction. thanks!
Turns out I was getting an exception from the call because I was going past the default quota for sizes of messages. I added a try/except block and determined this was the problem. Then cutting the message size down (I am just testing so dont want to mess with the quota) fixed the issue.

How to Have a Detailed Trace of a Slim Application Error

I have 10000 lines of code outlining routes of my API implemented using the Slim Framework. However, I got an error message preg_match(): Compilation failed: two named subpatterns have the same name at offset 89. The problem is, I got the stack trace referring to this statement preg_match('/cost-centers...', '/overview/funds...', NULL) at the Slim Route.php. Now that my URLs are lengthy, I can't pinpoint which of the URLs have the same name.
Is there any way to have a more detailed stack trace instead of displaying these shortened format?
Thanks to mgansler for this tip.
I just used a custom error handler with PHP Exception::getTrace() function. I also turned the Slim's default debugging off to ensure that the custom error handler is invoked.
Code goes like this:
$app = new \Slim\Slim(array(
'debug' => false
));
$app->error(function (\Exception $e) use ($app) {
//enter manipulation of $e->getTrace()
//or just var_dump($e->getTrace()) but the format would be chaotic
});

making a GET request to a webservice from the playframework 2.0

I'm trying to call a webservice from the play framework, and I think I'm doing it wrong. I have an example call to http://www.myweather2.com/developer/forecast.ashx?uac=eDKGlpcBQN&query=52.6%2C-4.4&output=xml
A snippet from what I'm trying from the playframework is the following:
val response = WS.url("http://www.myweather2.com/developer/forecast.ashx?uac=eDKGlpcBQN&query=52.6%2C-4.4&output=xml").get.get()
val body = response.getBody
When I call this, the body consists of "useraccount does not exist". When I just put this url in a browser, I get the response I'm looking for. What am I doing wrong here?
For some reason, I was getting WS from the wrong import. When I fixed the imports to import play.api.libs.ws.WS, it worked. I'm still amazed it worked halfway with the wrong import
Don't know about "useraccount does not exist" but this seems to work:
val promise = WS.url("http://www.myweather2.com/developer/forecast.ashx?uac=eDKGlpcBQN&query=52.6%2C-4.4&output=xml").get()
val body = promise.value.get.body
Edit: Removed the space.
Also make sure your editor is not inserting a \n or \r after ?
I know this is old, but I just solved this problem while trying to do the same thing - getting the same results.
GET variables must be passed with WS.url("http://...").setQueryParameter(key, value)
Example:
val promise = WS.url("http://www.myweather2.com/developer/forecast.ashx").setQueryParameter("uac", "eDKGlpcBQN").setQueryParameter("query", "52.6%2C-4.4").setQueryParameter("output", "xml").get()
Annoying, but a relatively simple fix.