Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    Ryman
    @Ryman
    @ruizander Thanks for the links!
    Ryman
    @Ryman
    It looks like in general this is talking about unchecked exceptions, which rust doesn't have, we have panic, but that should only be used in extreme cases (often highlighting a bug). In the case of panic the thread currently dies (we would need to wait for catch_panic to stabilize to allow custom behaviour here)
    Matt Banister
    @mattbanister
    @ruizander @Ryman It seems to me that the idiom for dealing with operational errors is just different. Javascript operations that may fail take a callback of the form function (err, res) {...} and rust operations should return a result type that you can match against.
    match res { 
        Ok(v) => // do something with v, 
        Err(err) => // handler err
     }
    Ryman
    @Ryman
    @mattbanister Pretty much yeah, for 'operational errors' you'll want to do the same as the linked article, handle it, or propagate the type. In nickel you may also want to use try_with!, as seen in an old example here: https://github.com/Ryman/nickel-todo-backend/blob/master/src/main.rs#L77
    @ruizander If you have a more concrete example of what you want to handle, I can take a look :)
    Matt Banister
    @mattbanister
    Ah okay thanks @Ryman that makes sense
    Off topic but I went to a meetup about this project last night : http://maidsafe.net/. Ambitious project (it basically aims to be a replacement for the internet) and their entire codebase is rust.
    Ryman
    @Ryman
    @mattbanister Cool, I meant to visit one of their meetups in London but got sidetracked. The meetup was good?
    ruizander
    @ruizander
    @Ryman I dont' have right now, but I'm learning rust and nickel in order to port a node.js website that I'm currently working...
    Ryman
    @Ryman
    ok! :+1:
    Matt Banister
    @mattbanister
    @Ryman yeah their platform is interesting. The idea is a kind of distributed computation/storage platform based on distributed hash tables with rewards built in for participating. It has some similarities with tor, blockchain and bittorrent if that makes any sense
    Also interesting that all of their code has been rewritten in rust
    ruizander
    @ruizander
    @Ryman i'd like to ask: if you have a nickel app that starts and connects to a database server and, after some queries, the database server goes offline... what kind of error does the nickel app throw?
    Ryman
    @Ryman
    @ruizander Well, depends on how your database code deals with things :P But currently, the two that I know of nickel-sqlite and nickel-postgres will panic due to unwraps, which should 500 but doesn't (see hyperium/hyper#676 as a proposed fix for handling panics better in hyper). You can implement this behavior yourself with a frontloaded Middleware which sets the default StatusCode to 500 or whatever you need, and that will be used if things panic.
    Lorenz Brun
    @lorenz
    Is there any doc on how to do error handling in nickel?
    Simon Persson
    @SimonPersson
    @lorenz Yes! The doc mentions how to create a NickelError here: http://docs.nickel.rs/nickel/struct.NickelError.html. The examples show how to register custom handlers for different error codes: https://github.com/nickel-org/nickel.rs/blob/master/examples/example.rs#L127. Is that what you wanted to do?
    Masood Ahmed
    @masoodahm
    hi, I know in nickel a middlware have a different meaning than other frameworks, but is there a mechanism to have a function that runs before every get and post to check if the user is logged in or not, if he/she is then continue to the handler else redirect to login
    ruizander
    @ruizander
    @Ryman i'd like to understand better the lifetime concepts on rust... although I've read the book, I think I need some exercises to really understand this... could you please help?
    I was trying to understand when to use str / String, and I've noticed &'a str a lot, but I don't get it..
    Lorenz Brun
    @lorenz
    @SimonPersson Thanks! I'll look into it.
    Simon Persson
    @SimonPersson
    @masoodahm I replied in the issue you opened :)
    @ruizander Is there something that feels particularly confusing about lifetimes?
    @ruizander Regarding String/&str, you generally want to use &str, simply because you can create a &str cheaply from a String, but not the other way around.
    Simon Persson
    @SimonPersson
    Of course there are times when you really need an owned string, then you must use String (and the compiler will tell you if you need to).
    Masood Ahmed
    @masoodahm
    yes thanks @SimonPersson a little bit more help, if you don't mind, the mound example on github says " * the request uri must be reset so that it can be matched against other middleware."
    how do i reset the request uri
    Simon Persson
    @SimonPersson
    @ruizander Perhaps this guide to learning Rust by writing a bunch of linked lists can be helpful to understand references and lifetimes in Rust?
    @masoodahm Aha, that's just a justification for why we that test case exists. It just means that after the middleware is incoked, the request uri is reset to whatever it was before. This is due to the nature of the mount middleware. See the actual implementation here.
    Masood Ahmed
    @masoodahm
    ok how do I redirect in the middleware of server.mount
    if i do res.redirect, it says use of moved value res
    here is what I have tried so far http://is.gd/1P9dYA
    Simon Persson
    @SimonPersson
    @masoodahm But, do you really want to use mount? I only meant for you to look at it as an example of how to run some code before a middleware is reached. You probably want some other logic, where you send users to different pages depending on whether they're logged in or not, no?
    ruizander
    @ruizander
    @SimonPersson thanks for the link! I'm trying....
    @SimonPersson Yes, I've noticed that &'str is used more often, but I didn't found out the reason... is there any special case that String is more recommended? I've done some exercises using Json on ToJson impl, and it only worked using String...
    Simon Persson
    @SimonPersson
    @ruizander Consider the case where you have a function fn a(foo: &str) -> &str, and in this function you want to call a function fn b(bar: String) -> String with foo as the parameter. In this case you will have to write foo.to_owned() which will have to copy every character to create a String, which is undesirable for performance reason. That is why we never actually take String as a parameter unless we really need an owned string.
    @ruizander A reason for wanting an owned string can be that you want to resize the string. Then it won't be enough to have a borrowed slice, we have to own the string.
    @ruizander Another good reason could be that you want to create a new string in a function and return it, then it makes no sense to return a borrowed slice (who would be the owner after the function that creates the string returns?).
    Ryman
    @Ryman
    @masoodahm try return res.redirect("/login"), but as Simon says, you might want to just use a regular handler, e.g. server.use(middleware! { .. your code.. }), which will run on all requests (unless you add middleware before that one).
    dopin
    @dopin
    Hi, I've just started using nicker.rs. But I'm stuck on using server.post which returns 404...
    server.post("/webhooks", middleware! { |request, response| // ...
    curl -X POST http://localhost:6969/webhooks returns 404, I meant.
    Maybe I'm doing something wrong. Can anyone help me? :sob:
    dopin
    @dopin
    I could handle it by doing server.post("**") anyway..
    Masood Ahmed
    @masoodahm
    yes @SimonPersson your are right I didn't need mount, anyways thanks guys I got it, I was under the impression that if a single middleware handler is found the server does not process further, but apparently that is not the case. . nicely architectured guys I can do middleware chaining with this, thanks again @Ryman and @SimonPersson
    Ryman
    @Ryman
    @dopin can you post more of the handler? My guess is that it's hitting the middleware but the middleware is returning a Continue (probably via ())
    dopin
    @dopin
    @Ryman Thanks for replying. I just tried to run on another machine and it worked. I don't know why... I use multirust and maybe I did some mistake yesterday...
    ruizander
    @ruizander
    @Ryman I've been doing some exercises on using rust datatypes, but I confess the compiler is fighting bravely against me... :worried: I'm trying to convert some datatypes from postgres, but I haven't find a good approach doing so...
    ruizander
    @ruizander
    here is what i'm trying to do: I have a struct with one field called data: String, and I have a field comming from the database on json format... I though it would be Option<Json>, so I'm trying to convert this Json to String, but no way till now...
            data: match row.get_opt("data") {
              Ok(info) => { info.to_string() },
              Err(err) => { println!("Error: {:?}", err); "".to_string() }
            }
    I could do using an ugly way, though...
          let data: Option<Json> = row.get_opt("data").unwrap();
          let content;
          match data {
            Some(y) => { content = y.to_string() },
            None => { content = "{}".to_string() }
          }
    but I think unwrapping the field is not the right way to handle, 'cause the field on the database does not require any value... i should simply convert null field to something like "".to_string()