@@ -31,18 +31,16 @@ use crate::storage::{LogStream, StorageDir};
3131use crate :: { event, stats} ;
3232use crate :: { metadata, validator} ;
3333
34- use self :: error:: StreamError ;
34+ use self :: error:: { CreateStreamError , StreamError } ;
3535
3636pub async fn delete ( req : HttpRequest ) -> Result < impl Responder , StreamError > {
3737 let stream_name: String = req. match_info ( ) . get ( "logstream" ) . unwrap ( ) . parse ( ) . unwrap ( ) ;
38- validator:: stream_name ( & stream_name) ?;
39-
40- let objectstore = CONFIG . storage ( ) . get_object_store ( ) ;
4138
4239 if !metadata:: STREAM_INFO . stream_exists ( & stream_name) {
4340 return Err ( StreamError :: StreamNotFound ( stream_name) ) ;
4441 }
4542
43+ let objectstore = CONFIG . storage ( ) . get_object_store ( ) ;
4644 objectstore. delete_stream ( & stream_name) . await ?;
4745 metadata:: STREAM_INFO . delete_stream ( & stream_name) ;
4846 event:: STREAM_WRITERS . delete_stream ( & stream_name) ;
@@ -269,27 +267,14 @@ fn remove_id_from_alerts(value: &mut Value) {
269267 }
270268}
271269
272- // Check if the stream exists and create a new stream if doesn't exist
273- pub async fn create_stream_if_not_exists ( stream_name : & str ) -> Result < ( ) , StreamError > {
274- if metadata:: STREAM_INFO . stream_exists ( stream_name) {
275- return Ok ( ( ) ) ;
276- }
277-
278- create_stream ( stream_name. to_string ( ) ) . await
279- }
280-
281- pub async fn create_stream ( stream_name : String ) -> Result < ( ) , StreamError > {
270+ pub async fn create_stream ( stream_name : String ) -> Result < ( ) , CreateStreamError > {
282271 // fail to proceed if invalid stream name
283272 validator:: stream_name ( & stream_name) ?;
284273
285274 // Proceed to create log stream if it doesn't exist
286275 let storage = CONFIG . storage ( ) . get_object_store ( ) ;
287- if let Err ( e) = storage. create_stream ( & stream_name) . await {
288- // Fail if unable to create log stream on object store backend
289- return Err ( StreamError :: Custom {
290- msg : format ! ( "failed to create log stream {stream_name} due to err: {e}" ) ,
291- status : StatusCode :: INTERNAL_SERVER_ERROR ,
292- } ) ;
276+ if let Err ( err) = storage. create_stream ( & stream_name) . await {
277+ return Err ( CreateStreamError :: Storage { stream_name, err } ) ;
293278 }
294279 metadata:: STREAM_INFO . add_stream ( stream_name. to_string ( ) ) ;
295280
@@ -308,9 +293,20 @@ pub mod error {
308293 } ;
309294
310295 #[ derive( Debug , thiserror:: Error ) ]
311- pub enum StreamError {
296+ pub enum CreateStreamError {
312297 #[ error( "Stream name validation failed due to {0}" ) ]
313298 StreamNameValidation ( #[ from] StreamNameValidationError ) ,
299+ #[ error( "failed to create log stream {stream_name} due to err: {err}" ) ]
300+ Storage {
301+ stream_name : String ,
302+ err : ObjectStorageError ,
303+ } ,
304+ }
305+
306+ #[ derive( Debug , thiserror:: Error ) ]
307+ pub enum StreamError {
308+ #[ error( "{0}" ) ]
309+ CreateStream ( #[ from] CreateStreamError ) ,
314310 #[ error( "Log stream {0} does not exist" ) ]
315311 StreamNotFound ( String ) ,
316312 #[ error( "Log stream is not initialized, send an event to this logstream and try again" ) ]
@@ -341,7 +337,12 @@ pub mod error {
341337 impl actix_web:: ResponseError for StreamError {
342338 fn status_code ( & self ) -> http:: StatusCode {
343339 match self {
344- StreamError :: StreamNameValidation ( _) => StatusCode :: BAD_REQUEST ,
340+ StreamError :: CreateStream ( CreateStreamError :: StreamNameValidation ( _) ) => {
341+ StatusCode :: BAD_REQUEST
342+ }
343+ StreamError :: CreateStream ( CreateStreamError :: Storage { .. } ) => {
344+ StatusCode :: INTERNAL_SERVER_ERROR
345+ }
345346 StreamError :: StreamNotFound ( _) => StatusCode :: NOT_FOUND ,
346347 StreamError :: Custom { status, .. } => * status,
347348 StreamError :: UninitializedLogstream => StatusCode :: METHOD_NOT_ALLOWED ,
0 commit comments