I use some delegate templates in the project and I use same approach but in some case i have exception.
This is the exception I got:
Exception is
com.google.template.soy.tofu.SoyTofuException: Found no active impl for delegate call to 'components.TemplateName'
Could somebody explain what is the best way to use delegate templates in soy files.
I found where was my mistake. I was missed ".delegate" in the end of delcall call. Here is some simple scenario:
{namespace somescope}
{template .Template}
{#param data: ?}
{#param variant: string}
{delcall somescope.TransportLeg.delegate data="all" variant="$variant"/}
{/template}
{deltemplate somescope.TransportLeg.delegate variant="'admin'"}
{#param data: ?}
<h1>Hello {$data.hello}edit</h1>
{/deltemplate}
{deltemplate somescope.TransportLeg.delegate variant="'user'"}
{#param data: ?}
<h1>Hello {$data.hello}</h1>
{/deltemplate}
Related
I'm trying to build a simple rest api in Lucerne, but the clack:call method fails if the json is malformed. So, I extended the bass-app class and added an around method:
(defclass tracker-app (base-app) ()
(:documentation "An extension of lucerne's base app to control behavior"))
(defmethod clack:call :around ((app tracker-app) env)
(handler-case (call-next-method)
(fast-http:cb-message-complete (e)
(vom:error "could not build message body: ~a" e)
(respond nil :status 400))
(:no-error (res) res)))
(defapp server :class 'tracker-app)
(start server :server woo)
But the parse error continues to crash the server.
I don't know much about clos, so I'm worried I've misunderstood how to catch errors in this context.
Edit: Added start information
Edit: Added stack trace
Assuming *features* does not contain :catch-any-error, here is a complete test case:
(ql:quickload :lucerne)
(defpackage :so.lucerne (:use :cl :lucerne))
(in-package :so.lucerne)
(defclass tracker-app (base-app) ()
(:documentation "An extension of lucerne's base app to control behavior"))
(defmethod clack:call :around ((app tracker-app) env)
(handler-case (call-next-method)
(fast-http:cb-message-complete (e)
(warn "could not build message body: ~a" e)
(respond nil :status 400))
#+catch-any-error
(error (e) (break "BREAK with ~a" e))
(:no-error (res) res)))
(defmethod clack:call ((app tracker-app) env)
(error "Oh No"))
(defapp server :class 'tracker-app)
(start server :server :woo)
When I try to load localhost:8000, the following error is shown:
Callback Error: the message-complete callback failed
Oh No
[Condition of type FAST-HTTP.ERROR:CB-MESSAGE-COMPLETE]
Pressing Enter on [Condition of type FAST-HTTP.ERROR:CB-MESSAGE-COMPLETE] gives:
#<FAST-HTTP.ERROR:CB-MESSAGE-COMPLETE {10048315C3}>
--------------------
The object is a CONDITION of type FAST-HTTP.ERROR:CB-MESSAGE-COMPLETE.
FORMAT-CONTROL: NIL
FORMAT-ARGUMENTS: NIL
DESCRIPTION: "the message-complete callback failed"
ERROR: #<SIMPLE-ERROR "Oh No" {1004831583}>
The error wraps another error.
Now if I (push :catch-any-error *features*) and recompile the above method, the same test makes the code reach the (break ...) statement, which is shown as BREAK with Oh No.
Explanation
No fast-http:cb-message-complete is caught, and in fact no such condition is signaled at this point; instead at this location we only can catch the specific error that was signaled. It is only higher up in the call stack that errors are wrapped inside fast-http:cb-message-complete errors.
Solution
In your case you can directly catch jonathan.error:<jonathan-error> (unusual naming convention, but ok), the base class of all errors in the jonathan library (you could catch the specific error type, but then you risk missing some other cases).
[This answer is wrong: fast-http.error:cb-message-complete & fast-http:cb-message-complete seem to be the same symbol. I am leaving it here for posterity.]
You are not handling the right condition (and in fact I'm not sure you're handling a condition which exists at all, which I'd expect the system to have warned about perhaps, although perhaps it can't).
You need to handle fast-http.error:cb-message-complete, but your handler specifies fast-http:cb-message-complete. The first of these is a condition (defined here, while the second is I think implicitly define here and is not a condition but the name of a function I think.
A more general trick is to try to handle some much-too-general error: you will probably end up handling things you don't know how to handle, but if your handler gets invoked you know that the call stack looks like you think it does when the error is signalled. Then you can start handling the errors you actually care about. In this case that probably means fast-http.error:callback-error I think.
I'm trying to serialize JSON using serde macros but I only get this error:
src/models/pulse.rs:89:28: 89:49 error: the trait bound `models::pulse::Pulse: serde::ser::Serialize` is not satisfied [E0277]
src/models/pulse.rs:89 if let Ok(bulk_string) = serde_json::to_string(&self) {
^~~~~~~~~~~~~~~~~~~~~
src/models/pulse.rs:89:28: 89:49 help: run `rustc --explain E0277` to see a detailed explanation
src/models/pulse.rs:89:28: 89:49 note: required because of the requirements on the impl of `serde::ser::Serialize` for `&mut models::pulse::Pulse`
src/models/pulse.rs:89:28: 89:49 note: required by `serde_json::to_string`
I don't seem to have anything wrong in the code.
#[derive(Serialize, Deserialize)]
pub struct Pulse {
#[serde(skip_serializing_if="Option::is_none")]
id: Option<u64>,
#[serde(skip_serializing_if="Option::is_none")]
category: Option<i64>,
#[serde(skip_serializing_if="Option::is_none")]
title: Option<String>,
}
Is it an dependency error? Or is the something wrong with my code? I use
[dependencies.serde]
git = "https://github.com/serde-rs/serde.git"
[dependencies.serde_macros]
git = "https://github.com/serde-rs/serde.git"
[dependencies.serde_json]
git = "https://github.com/serde-rs/json.git"
I got a question why I use git dependencies. The reason is that I get:
/.cargo/registry/src/github.com-1ecc6299db9ec823/aster-
0.18.0/src/mac.rs:6:5: 6:39 error: unresolved import `syntax::feature_gate::GatedCfgAttr`. There is no `GatedCfgAttr` in `syntax::feature_gate`. Did you mean to use `GatedCfg`? [E0432]
/.cargo/registry/src/github.com-1ecc6299db9ec823/aster-0.18.0/src/mac.rs:6 use syntax::feature_gate::GatedCfgAttr;
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/.cargo/registry/src/github.com-1ecc6299db9ec823/aster-0.18.0/src/mac.rs:6:5: 6:39 help: run `rustc --explain E0432` to see a detailed explanation
Compiling num-traits v0.1.32
/.cargo/registry/src/github.com-1ecc6299db9ec823/aster-0.18.0/src/mac.rs:113:18: 113:81 error: this function takes 4 parameters but 5 parameters were supplied [E0061]
/.cargo/registry/src/github.com-1ecc6299db9ec823/aster-0.18.0/src/mac.rs:113 let mut cx = ExtCtxt::new(sess, cfg, ecfg, feature_gated_cfgs, macro_loader);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/.cargo/registry/src/github.com-1ecc6299db9ec823/aster-0.18.0/src/mac.rs:113:18: 113:81 help: run `rustc --explain E0061` to see a detailed explanation
/.cargo/registry/src/github.com-1ecc6299db9ec823/aster-0.18.0/src/mac.rs:113:18: 113:81 note: the following parameter types were expected: &syntax::parse::ParseSess, std::vec::Vec<syntax::ptr::P<syntax::codemap::Spanned<syntax::ast::MetaItemKind>>>, syntax::ext::expand::ExpansionConfig<'_>, &mut syntax::ext::base::MacroLoader
I think the problem could be that you need to use nightly.
As of rustc 1.20.0-nightly, serde 1.0.11 and serde-json 1.0.2, this code compiles without a problem.
On a side note, you really should use crates.io dependencies instead of git ones.
I am creating an API that uses JSON to communicate back/forth with external view apps (Angular). In a lot of API actions, I return a JSON response that is 99% the same as the error one below:
# controller
def create
#record = Record.new(record_params)
if #record.save
#record
else
render json: {
error: {
type: "invalid_request",
message: "Could not create record. Params: #{record_params}",
errors: #record.errors.messages
}
}, status: 404
end
end
Is there a convenient way to DRY this up? I ask specifically because I know certain methods such as the render only work in controller classes because they are inherited.
I'm thinking about something like the following:
render json: API::ErrorObject.call(#record, record_params), status: 404
And in that class it would be:
class API::ErrorObject
self.call(object, params)
{
error: {
type: "invalid_request",
message: "Could not create record. Params: #{record_params}",
errors: object.errors.messages
}
}
end
end
I think that would work, but is there an even cleaner way to abstract away some of this behavior? The API is fairly large, so there are 30+ places where very similar code will reside. I know that someday someone will request an addition to the API responses, and having a single place to update this would be a lot better than 30...
The best solution I've found so far is to bring the V back into MVC by using a tool like Jbuilder.
Using this you can really DRY up your code similar to what you do using partials in ERB.
I am trying to build a REST application using sypne. I want to receive a JSON document (but not with with the method called as the name of the object. I give en example to clarify:
I make a POST to the URL http://localhost/root with the following content:
{ "id": 1, "desc": "number" }
And I have my spyne application:
class HelloWorldService(ServiceBase):
#rpc(_returns=AnyDict)
def root(ctx):
json_in=""
for data in ctx.in_string:
json_in = json_in + data
return json.loads(json_in)
application = Application([HelloWorldService],
tns='com.hello.webservices',
in_protocol=HttpRpc(validator='soft'),
out_protocol=JsonDocument()
)
And I know there has to be a better way of doing this! Any pointers to documentation or solution appreciated in advance!
Regards
Spyne doesn't have that functionality ouf the box, you need to implement your own protocol for that.
It's pretty easy though. Off the top of my head:
class HybridHttpJsonDocument(JsonDocument):
def create_in_document(self, ctx):
super(HybridHttpJsonDocument, self).create_in_document(ctx)
# get url fragment from ctx.transport.req_env
ctx.in_document = {url_fragment: ctx.in_document}
That's all I can think of that you need to do. Please report back with your results!
Best regards,
I am using symfony 1.4, to create my project with propel as ORM. i want to get the response in JSON format, when i call a url. I have set the headers to "application/json" but it is not working, i am getting the response back in HTML format..which i am not able to decode. How can we set content-type in symfony??
example code:
Action-
public function executeIndex(sfWebRequest $request)
{
$data_array=array("name" => "harry", "mobile" => "9876543210");
$data_json=json_encode($data_array);
$this->output=$data_json;
}
View-
<?php
header('Content-type: application/json');
echo $output;
?>
ok i got where i was going wrong..yhe code should have been..
Action-
public function executeIndex(sfWebRequest $request)
{
$this->getResponse()->setContentType('application/json');
$data_array=array("name" => "harry", "mobile" => "9876543210");
$data_json=json_encode($data_array);
return $this->renderText($data_json);
}
this code has worked for me, please post if any better solution you have got..
You can also define this in the view.yml (apps/{app_name}/modules/{module_name}/config/view.yml) file for the module.
indexSuccess:
has_layout: false
http_metas:
content-type: application/json
Oh ! As an alternative, I'd just say I have been using the routing system, which provides a pretty neat way to get it done:
-> In your routing.yml
json_test:
url: /test
class: sfRequestRoute
param: { module: test, action: index, sf_format: json }
Then, the frameworks will automatically pick up you view index.json.php, which you have to create. As mentioned above, you can generate the content in the action with json_encode, though there are arguments to put it in the view.
Now... Ok, if you are interested in picking some more about this, have a look at the "Practical symfony" tutorial: Day 15: Web Services
There's some good stuff down there !
In a REST style, just add .format to the URI, create the relative template and Symfony Routing system does the rest of the work for us.