@@ -31,18 +31,16 @@ use crate::storage::{LogStream, StorageDir};
31
31
use crate :: { event, stats} ;
32
32
use crate :: { metadata, validator} ;
33
33
34
- use self :: error:: StreamError ;
34
+ use self :: error:: { CreateStreamError , StreamError } ;
35
35
36
36
pub async fn delete ( req : HttpRequest ) -> Result < impl Responder , StreamError > {
37
37
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 ( ) ;
41
38
42
39
if !metadata:: STREAM_INFO . stream_exists ( & stream_name) {
43
40
return Err ( StreamError :: StreamNotFound ( stream_name) ) ;
44
41
}
45
42
43
+ let objectstore = CONFIG . storage ( ) . get_object_store ( ) ;
46
44
objectstore. delete_stream ( & stream_name) . await ?;
47
45
metadata:: STREAM_INFO . delete_stream ( & stream_name) ;
48
46
event:: STREAM_WRITERS . delete_stream ( & stream_name) ;
@@ -269,27 +267,14 @@ fn remove_id_from_alerts(value: &mut Value) {
269
267
}
270
268
}
271
269
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 > {
282
271
// fail to proceed if invalid stream name
283
272
validator:: stream_name ( & stream_name) ?;
284
273
285
274
// Proceed to create log stream if it doesn't exist
286
275
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 } ) ;
293
278
}
294
279
metadata:: STREAM_INFO . add_stream ( stream_name. to_string ( ) ) ;
295
280
@@ -308,9 +293,20 @@ pub mod error {
308
293
} ;
309
294
310
295
#[ derive( Debug , thiserror:: Error ) ]
311
- pub enum StreamError {
296
+ pub enum CreateStreamError {
312
297
#[ error( "Stream name validation failed due to {0}" ) ]
313
298
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 ) ,
314
310
#[ error( "Log stream {0} does not exist" ) ]
315
311
StreamNotFound ( String ) ,
316
312
#[ error( "Log stream is not initialized, send an event to this logstream and try again" ) ]
@@ -341,7 +337,12 @@ pub mod error {
341
337
impl actix_web:: ResponseError for StreamError {
342
338
fn status_code ( & self ) -> http:: StatusCode {
343
339
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
+ }
345
346
StreamError :: StreamNotFound ( _) => StatusCode :: NOT_FOUND ,
346
347
StreamError :: Custom { status, .. } => * status,
347
348
StreamError :: UninitializedLogstream => StatusCode :: METHOD_NOT_ALLOWED ,
0 commit comments