Skip to content
This repository has been archived by the owner on Jun 1, 2022. It is now read-only.

Commit

Permalink
Add invalid content error (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
Owen Rumney committed Nov 28, 2021
1 parent d212bd7 commit 87f4273
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 16 deletions.
36 changes: 36 additions & 0 deletions internal/app/cfsec/parser/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package parser

import "fmt"

type ErrNotCloudFormation struct {
source string
}

func NewErrNotCloudFormation(source string) *ErrNotCloudFormation {
return &ErrNotCloudFormation{
source: source,
}
}

func (e *ErrNotCloudFormation) Error() string {
return fmt.Sprintf("The file %s is not CloudFormation", e.source)
}

type ErrInvalidContent struct {
source string
err error
}

func NewErrInvalidContent(source string, err error) *ErrInvalidContent {
return &ErrInvalidContent{
source: source,
err: err,
}
}
func (e *ErrInvalidContent) Error() string {
return fmt.Sprintf("Invalid content in file: %s", e.source)
}

func (e *ErrInvalidContent) Reason() error {
return e.err
}
20 changes: 4 additions & 16 deletions internal/app/cfsec/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,10 @@ import (
"gopkg.in/yaml.v3"
)

type ErrNotCloudFormation struct {
source string
}

func NewErrNotCloudFormation(source string) *ErrNotCloudFormation {
return &ErrNotCloudFormation{
source: source,
}
}

func (e *ErrNotCloudFormation) Error() string{
return fmt.Sprintf("The file %s is not CloudFormation", e.source)
}

// Parser ...
type Parser struct{
type Parser struct {
parameters map[string]Parameter
}

Expand Down Expand Up @@ -63,7 +51,7 @@ func (p *Parser) ParseFiles(filepaths ...string) (FileContexts, error) {
if err != nil {
return err
}
defer func() {_ = file.Close()}()
defer func() { _ = file.Close() }()

context, err := p.Parse(file, path)
if err != nil {
Expand Down Expand Up @@ -113,11 +101,11 @@ func (p *Parser) Parse(reader io.Reader, source string) (*FileContext, error) {

if strings.HasSuffix(strings.ToLower(source), ".json") {
if err := jfather.Unmarshal(content, context); err != nil {
return nil, fmt.Errorf("source '%s' contains invalid JSON: %w", source, err)
return nil, NewErrInvalidContent(source, err)
}
} else {
if err := yaml.Unmarshal(content, context); err != nil {
return nil, fmt.Errorf("source '%s' contains invalid YAML: %w", source, err)
return nil, NewErrInvalidContent(source, err)
}
}

Expand Down

0 comments on commit 87f4273

Please sign in to comment.