error types
This commit is contained in:
parent
9fe0caeab5
commit
fccdb59173
@ -19,3 +19,5 @@ postgres = { version = "0.15", features = ["with-uuid"] }
|
|||||||
r2d2 = "*"
|
r2d2 = "*"
|
||||||
r2d2_postgres = "*"
|
r2d2_postgres = "*"
|
||||||
|
|
||||||
|
failure = "0.1"
|
||||||
|
|
||||||
|
|||||||
@ -176,15 +176,12 @@ impl Cryp {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn generate(params: GenerateParams) -> Vec<u8> {
|
pub fn generate(params: GenerateParams) -> Cryp {
|
||||||
let level_two = Cryp::new()
|
Cryp::new()
|
||||||
.named("hatchling".to_string())
|
.named("hatchling".to_string())
|
||||||
.level(params.level)
|
.level(params.level)
|
||||||
.learn(Skill::Stoney)
|
.learn(Skill::Stoney)
|
||||||
.create();
|
.create()
|
||||||
|
|
||||||
to_vec(&level_two)
|
|
||||||
.expect("couldn't serialize cryp")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -15,6 +15,9 @@ extern crate serde_cbor;
|
|||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate serde_derive;
|
extern crate serde_derive;
|
||||||
|
|
||||||
|
extern crate failure;
|
||||||
|
// #[macro_use] extern crate failure_derive;
|
||||||
|
|
||||||
mod cryp;
|
mod cryp;
|
||||||
mod battle;
|
mod battle;
|
||||||
mod net;
|
mod net;
|
||||||
|
|||||||
@ -9,8 +9,7 @@ use r2d2_postgres::{TlsMode, PostgresConnectionManager};
|
|||||||
|
|
||||||
pub type Db = PooledConnection<PostgresConnectionManager>;
|
pub type Db = PooledConnection<PostgresConnectionManager>;
|
||||||
|
|
||||||
use cryp::{generate};
|
use rpc::{Rpc};
|
||||||
use rpc::{Rpc,RpcMessage};
|
|
||||||
|
|
||||||
struct Server {
|
struct Server {
|
||||||
out: Sender,
|
out: Sender,
|
||||||
@ -28,11 +27,12 @@ impl Handler for Server {
|
|||||||
let db = self.db.get().expect("unable to get db connection");
|
let db = self.db.get().expect("unable to get db connection");
|
||||||
match self.rpc.receive(msg, db) {
|
match self.rpc.receive(msg, db) {
|
||||||
Ok(reply) => {
|
Ok(reply) => {
|
||||||
println!("{:?}", reply);
|
let response = to_vec(&reply)
|
||||||
self.out.send(reply)
|
.expect("failed to serialize response");
|
||||||
|
self.out.send(response)
|
||||||
},
|
},
|
||||||
Err(_e) => {
|
Err(e) => {
|
||||||
let response = to_vec(&"there was an error")
|
let response = to_vec(&e.to_string())
|
||||||
.expect("failed to serialize error response");
|
.expect("failed to serialize error response");
|
||||||
self.out.send(response)
|
self.out.send(response)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,15 +1,16 @@
|
|||||||
use std::result::Result as StdResult;
|
|
||||||
use ws::{Message};
|
use ws::{Message};
|
||||||
use serde_cbor::{from_slice};
|
use serde_cbor::{from_slice};
|
||||||
|
use failure::Error;
|
||||||
|
use failure::err_msg;
|
||||||
|
|
||||||
use net::Db;
|
use net::Db;
|
||||||
use cryp::generate;
|
use cryp::{Cryp, generate};
|
||||||
use user::{create};
|
use user::{User, create};
|
||||||
|
|
||||||
pub struct Rpc;
|
pub struct Rpc;
|
||||||
|
|
||||||
impl Rpc {
|
impl Rpc {
|
||||||
pub fn receive(&self, msg: Message, db: Db) -> StdResult<Vec<u8>, RpcError> {
|
pub fn receive(&self, msg: Message, db: Db) -> Result<RpcResult, Error> {
|
||||||
// consume the ws data into bytes
|
// consume the ws data into bytes
|
||||||
let data = msg.into_data();
|
let data = msg.into_data();
|
||||||
|
|
||||||
@ -22,25 +23,32 @@ impl Rpc {
|
|||||||
match v.method.as_ref() {
|
match v.method.as_ref() {
|
||||||
"cryp_generate" => {
|
"cryp_generate" => {
|
||||||
match from_slice::<GenerateMsg>(&data) {
|
match from_slice::<GenerateMsg>(&data) {
|
||||||
Ok(v) => Ok(generate(v.params)),
|
Ok(v) => Ok(RpcResult::Cryp(generate(v.params))),
|
||||||
Err(e) => Err(RpcError::Parse),
|
Err(_e) => Err(err_msg("invalid params")),
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"account_create" => {
|
"account_create" => {
|
||||||
match from_slice::<AccountCreateMsg>(&data) {
|
match from_slice::<AccountCreateMsg>(&data) {
|
||||||
Ok(v) => Ok(create(v.params, db)),
|
Ok(v) => create(v.params, db),
|
||||||
Err(e) => Err(RpcError::Parse),
|
Err(_e) => Err(err_msg("invalid params")),
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
_ => Err(RpcError::UnknownMethod),
|
_ => Err(err_msg("unknown method")),
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Err(e) => Err(RpcError::Parse),
|
Err(_e) => Err(err_msg("invalid message")),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||||
|
pub enum RpcResult {
|
||||||
|
Cryp(Cryp),
|
||||||
|
User(User),
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||||
pub struct RpcMessage {
|
pub struct RpcMessage {
|
||||||
method: String,
|
method: String,
|
||||||
|
|||||||
@ -1,4 +1,3 @@
|
|||||||
use serde_cbor::to_vec;
|
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
use bcrypt::{DEFAULT_COST, hash};
|
use bcrypt::{DEFAULT_COST, hash};
|
||||||
use rand::{thread_rng, Rng};
|
use rand::{thread_rng, Rng};
|
||||||
@ -8,10 +7,13 @@ use std::iter;
|
|||||||
use std::str;
|
use std::str;
|
||||||
|
|
||||||
use net::Db;
|
use net::Db;
|
||||||
use rpc::{AccountCreateParams};
|
use rpc::{AccountCreateParams, RpcResult};
|
||||||
|
|
||||||
|
use failure::Error;
|
||||||
|
use failure::err_msg;
|
||||||
|
|
||||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||||
struct User {
|
pub struct User {
|
||||||
id: Uuid,
|
id: Uuid,
|
||||||
name: String,
|
name: String,
|
||||||
token: String,
|
token: String,
|
||||||
@ -24,11 +26,15 @@ struct UserEntry {
|
|||||||
token: String,
|
token: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn create(params: AccountCreateParams, db: Db) -> Vec<u8> {
|
static PASSWORD_MIN_LEN: usize = 12;
|
||||||
|
|
||||||
|
pub fn create(params: AccountCreateParams, db: Db) -> Result<RpcResult, Error> {
|
||||||
let id = Uuid::new_v4();
|
let id = Uuid::new_v4();
|
||||||
let password = hash(¶ms.password, DEFAULT_COST)
|
|
||||||
.expect("unable to hash password");
|
if params.password.len() < PASSWORD_MIN_LEN {
|
||||||
|
return Err(err_msg("password must be at least 12 characters"));
|
||||||
|
}
|
||||||
|
let password = hash(¶ms.password, DEFAULT_COST)?;
|
||||||
|
|
||||||
let mut rng = thread_rng();
|
let mut rng = thread_rng();
|
||||||
let token: String = iter::repeat(())
|
let token: String = iter::repeat(())
|
||||||
@ -49,9 +55,11 @@ pub fn create(params: AccountCreateParams, db: Db) -> Vec<u8> {
|
|||||||
RETURNING id, name, token;
|
RETURNING id, name, token;
|
||||||
";
|
";
|
||||||
|
|
||||||
let result = db
|
let tx = db.transaction()?;
|
||||||
.query(query, &[&user.id, &user.name, &user.password, &user.token])
|
|
||||||
.expect("user insert failed");
|
let result = tx
|
||||||
|
.query(query, &[&user.id, &user.name, &user.password, &user.token])?;
|
||||||
|
|
||||||
let returned = result.iter().next().expect("no row returned");
|
let returned = result.iter().next().expect("no row returned");
|
||||||
|
|
||||||
let entry = User {
|
let entry = User {
|
||||||
@ -62,5 +70,7 @@ pub fn create(params: AccountCreateParams, db: Db) -> Vec<u8> {
|
|||||||
|
|
||||||
println!("{:?}", entry);
|
println!("{:?}", entry);
|
||||||
|
|
||||||
to_vec(&entry).expect("serialising user failed")
|
tx.commit()?;
|
||||||
|
|
||||||
|
Ok(RpcResult::User(entry))
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user