2016-02-25 22:59:38 +00:00
package pathmatch
import (
"fmt"
)
2019-06-21 04:40:10 +00:00
// PatternSyntaxError is used to represent a specific kind of BadRequest error.
2016-02-25 22:59:38 +00:00
// Specifically, it represents a syntax error in the uncompiled pattern passed to the
// pathmatch.Compile() func.
//
// Example usage is as follows:
//
// pattern, err := pathmatch.Compile("/something/{there_is_a_syntax_error_in_this_pattern")
// if nil != err {
// switch err.(type) {
//
2019-06-21 04:40:10 +00:00
// case pathmatch.PatternSyntaxError: // ← Here we are detecting if the error returned was due to a syntax error, in the uncompiled pattern. Also note that it comes BEFORE the 'pathmatch.BadRequest' case; THAT IS IMPORTANT!
2016-02-25 22:59:38 +00:00
//
// fmt.Printf("The uncompiled pattern passed to pathmatch.Compile() had a syntax error in it. The error message describing the syntax error is....\n%s\n", err.Error())
// return
//
2019-06-21 04:31:53 +00:00
// case pathmatch.BadRequest:
2016-02-25 22:59:38 +00:00
//
// fmt.Printf("Something you did when you called pathmatch.Compile() caused an error. The error message was....\n%s\n", err.Error())
// return
//
2019-06-21 04:35:33 +00:00
// case pathmatch.InternalError:
2016-02-25 22:59:38 +00:00
//
// fmt.Printf("It's not your fault; it's our fault. Something bad happened internally when pathmatch.Compile() was running. The error message was....\n%s\n", err.Error())
// return
//
// default:
//
// fmt.Printf("Some kind of unexpected error happend: %v", err)
// return
// }
// }
2019-06-21 04:40:10 +00:00
type PatternSyntaxError interface {
2019-06-21 04:31:53 +00:00
BadRequest
2019-06-21 04:40:10 +00:00
PatternSyntaxError ( )
2016-02-25 22:59:38 +00:00
}
2019-06-21 04:40:10 +00:00
// internalPatternSyntaxError is the only underlying implementation that fits the
// PatternSyntaxError interface, in this library.
type internalPatternSyntaxError struct {
2016-02-25 22:59:38 +00:00
msg string
}
2019-06-21 04:40:10 +00:00
// newPatternSyntaxError creates a new internalPatternSyntaxError (struct) and
// returns it as a PatternSyntaxError (interface).
func newPatternSyntaxError ( format string , a ... interface { } ) PatternSyntaxError {
2016-02-25 22:59:38 +00:00
msg := fmt . Sprintf ( format , a ... )
2019-06-21 04:40:10 +00:00
err := internalPatternSyntaxError {
2016-02-25 22:59:38 +00:00
msg : msg ,
}
return & err
}
// Error method is necessary to satisfy the 'error' interface (and the
2019-06-21 04:40:10 +00:00
// PatternSyntaxError interface).
func ( err * internalPatternSyntaxError ) Error ( ) string {
2016-02-25 22:59:38 +00:00
s := fmt . Sprintf ( "Bad Request: Syntax Error: %s" , err . msg )
return s
}
2019-06-21 04:35:33 +00:00
// BadRequest method is necessary to satisfy the 'InternalError' interface.
2016-02-25 22:59:38 +00:00
// It exists to make this error type detectable in a Go type-switch.
2019-06-21 04:40:10 +00:00
func ( err * internalPatternSyntaxError ) BadRequest ( ) {
2016-02-25 22:59:38 +00:00
// Nothing here.
}
2019-06-21 04:40:10 +00:00
// PatternSyntaxError method is necessary to satisfy the 'PatternSyntaxError' interface.
2016-02-25 22:59:38 +00:00
// It exists to make this error type detectable in a Go type-switch.
2019-06-21 04:40:10 +00:00
func ( err * internalPatternSyntaxError ) PatternSyntaxError ( ) {
2016-02-25 22:59:38 +00:00
// Nothing here.
}