Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add the log-check route and remove unused ApiError variants/impls #209

Merged
merged 2 commits into from
Aug 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 7 additions & 23 deletions autoendpoint/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ pub enum ApiErrorKind {
#[error("Invalid message ID")]
InvalidMessageId,

#[error("{0}")]
Internal(String),

#[error("Invalid Authentication")]
InvalidAuthentication,

#[error("ERROR:Success")]
LogCheck,
}

impl ApiErrorKind {
Expand All @@ -138,12 +138,13 @@ impl ApiErrorKind {

ApiErrorKind::NoUser | ApiErrorKind::NoSubscription => StatusCode::GONE,

ApiErrorKind::LogCheck => StatusCode::IM_A_TEAPOT,

ApiErrorKind::Io(_)
| ApiErrorKind::Metrics(_)
| ApiErrorKind::Database(_)
| ApiErrorKind::EndpointUrl(_)
| ApiErrorKind::RegistrationSecretHash(_)
| ApiErrorKind::Internal(_) => StatusCode::INTERNAL_SERVER_ERROR,
| ApiErrorKind::RegistrationSecretHash(_) => StatusCode::INTERNAL_SERVER_ERROR,
}
}

Expand Down Expand Up @@ -178,7 +179,7 @@ impl ApiErrorKind {

ApiErrorKind::NoTTL => Some(111),

ApiErrorKind::Internal(_) => Some(999),
ApiErrorKind::LogCheck => Some(999),

ApiErrorKind::Io(_)
| ApiErrorKind::Metrics(_)
Expand Down Expand Up @@ -228,23 +229,6 @@ where
}
}

impl From<actix_web::error::BlockingError<ApiError>> for ApiError {
fn from(inner: actix_web::error::BlockingError<ApiError>) -> Self {
match inner {
actix_web::error::BlockingError::Error(e) => e,
actix_web::error::BlockingError::Canceled => {
ApiErrorKind::Internal("Db threadpool operation canceled".to_owned()).into()
}
}
}
}

impl From<ApiError> for HttpResponse {
fn from(inner: ApiError) -> Self {
ResponseError::error_response(&inner)
}
}

impl ResponseError for ApiError {
fn status_code(&self) -> StatusCode {
self.kind.status()
Expand Down
18 changes: 18 additions & 0 deletions autoendpoint/src/routes/health.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
//! Health and Dockerflow routes

use crate::db::error::DbResult;
use crate::error::{ApiErrorKind, ApiResult};
use crate::server::ServerState;
use actix_web::web::{Data, Json};
use actix_web::HttpResponse;
use reqwest::StatusCode;
use serde_json::json;
use std::thread;

/// Handle the `/health` and `/__heartbeat__` routes
pub async fn health_route(state: Data<ServerState>) -> Json<serde_json::Value> {
Expand Down Expand Up @@ -58,3 +61,18 @@ pub fn version_route() -> HttpResponse {
.content_type("application/json")
.body(include_str!("../../../version.json"))
}

/// Handle the `/v1/err` route
pub async fn log_check() -> ApiResult<String> {
error!(
"Test Critical Message";
"status_code" => StatusCode::IM_A_TEAPOT.as_u16(),
"errno" => 999,
);

thread::spawn(|| {
panic!("LogCheck");
});

Err(ApiErrorKind::LogCheck.into())
}
5 changes: 4 additions & 1 deletion autoendpoint/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use crate::middleware::sentry::sentry_middleware;
use crate::routers::adm::router::AdmRouter;
use crate::routers::apns::router::ApnsRouter;
use crate::routers::fcm::router::FcmRouter;
use crate::routes::health::{health_route, lb_heartbeat_route, status_route, version_route};
use crate::routes::health::{
health_route, lb_heartbeat_route, log_check, status_route, version_route,
};
use crate::routes::registration::{
get_channels_route, new_channel_route, register_uaid_route, unregister_channel_route,
unregister_user_route, update_token_route,
Expand Down Expand Up @@ -131,6 +133,7 @@ impl Server {
// Health checks
.service(web::resource("/status").route(web::get().to(status_route)))
.service(web::resource("/health").route(web::get().to(health_route)))
.service(web::resource("/v1/err").route(web::get().to(log_check)))
// Dockerflow
.service(web::resource("/__heartbeat__").route(web::get().to(health_route)))
.service(web::resource("/__lbheartbeat__").route(web::get().to(lb_heartbeat_route)))
Expand Down