reworking errors + now using a working dir for our converter

This commit is contained in:
Julien Neuhart
2018-03-30 09:19:37 +02:00
parent e847c86baa
commit a882b7e5c5
16 changed files with 203 additions and 241 deletions

View File

@@ -14,6 +14,7 @@ type key uint32
const (
contentTypeKey key = iota
converterKey
resultFilePathKey
)
func WithContentType(r *http.Request, contentType ghttp.ContentType) *http.Request {
@@ -24,6 +25,12 @@ func WithContentType(r *http.Request, contentType ghttp.ContentType) *http.Reque
return r
}
type contentTypeNotFoundError struct{}
func (e *contentTypeNotFoundError) Error() string {
return "The 'Content-Type' was not found in request context"
}
func GetContentType(r *http.Request) (ghttp.ContentType, error) {
ct, ok := r.Context().Value(contentTypeKey).(ghttp.ContentType)
if !ok {
@@ -41,6 +48,12 @@ func WithConverter(r *http.Request, converter *converter.Converter) *http.Reques
return r
}
type converterNotFoundError struct{}
func (e *converterNotFoundError) Error() string {
return "The converter was not found in request context"
}
func GetConverter(r *http.Request) (*converter.Converter, error) {
c, ok := r.Context().Value(converterKey).(*converter.Converter)
if !ok {
@@ -49,3 +62,26 @@ func GetConverter(r *http.Request) (*converter.Converter, error) {
return c, nil
}
func WithResultFilePath(r *http.Request, resultFilePath string) *http.Request {
ctx := r.Context()
ctx = context.WithValue(ctx, resultFilePathKey, resultFilePath)
r = r.WithContext(ctx)
return r
}
type resultFilePathNotFoundError struct{}
func (e *resultFilePathNotFoundError) Error() string {
return "The resultFilePath was not found in request context"
}
func GetResultFilePath(r *http.Request) (string, error) {
path, ok := r.Context().Value(resultFilePathKey).(string)
if !ok {
return "", &resultFilePathNotFoundError{}
}
return path, nil
}