-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add meaningful HTTP/gRPC codes (#69)
* Add meaningful HTTP status codes using gRPC * Update API docs with error status and responses * Add http/grpc status tests * Make codeForError function private * Update api proto formatting and version/license
- Loading branch information
Showing
11 changed files
with
1,579 additions
and
207 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,3 +19,4 @@ plugins: | |
opt: | ||
- logtostderr=true | ||
- allow_repeated_fields_in_body=true | ||
- disable_default_errors=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// Copyright © 2022 Meroxa, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package status | ||
|
||
import ( | ||
"github.com/conduitio/conduit/pkg/connector" | ||
"github.com/conduitio/conduit/pkg/foundation/cerrors" | ||
"github.com/conduitio/conduit/pkg/orchestrator" | ||
"github.com/conduitio/conduit/pkg/pipeline" | ||
"github.com/conduitio/conduit/pkg/processor" | ||
"google.golang.org/grpc/codes" | ||
grpcstatus "google.golang.org/grpc/status" | ||
) | ||
|
||
func PipelineError(err error) error { | ||
var code codes.Code | ||
|
||
switch { | ||
case cerrors.Is(err, pipeline.ErrNameMissing): | ||
code = codes.InvalidArgument | ||
case cerrors.Is(err, pipeline.ErrInstanceNotFound): | ||
code = codes.NotFound | ||
default: | ||
code = codeFromError(err) | ||
} | ||
|
||
return grpcstatus.Error(code, err.Error()) | ||
} | ||
|
||
func ConnectorError(err error) error { | ||
var code codes.Code | ||
|
||
switch { | ||
case cerrors.Is(err, connector.ErrInvalidConnectorType): | ||
code = codes.InvalidArgument | ||
case cerrors.Is(err, connector.ErrInstanceNotFound): | ||
code = codes.NotFound | ||
default: | ||
code = codeFromError(err) | ||
} | ||
|
||
return grpcstatus.Error(code, err.Error()) | ||
} | ||
|
||
func ProcessorError(err error) error { | ||
var code codes.Code | ||
|
||
switch { | ||
case cerrors.Is(err, orchestrator.ErrInvalidProcessorParentType): | ||
code = codes.InvalidArgument | ||
case cerrors.Is(err, processor.ErrInstanceNotFound): | ||
code = codes.NotFound | ||
default: | ||
code = codeFromError(err) | ||
} | ||
|
||
return grpcstatus.Error(code, err.Error()) | ||
} | ||
|
||
func codeFromError(err error) codes.Code { | ||
switch { | ||
case cerrors.Is(err, cerrors.ErrNotImpl): | ||
return codes.Unimplemented | ||
case cerrors.Is(err, cerrors.ErrEmptyID): | ||
return codes.InvalidArgument | ||
case cerrors.Is(err, pipeline.ErrPipelineRunning): | ||
return codes.FailedPrecondition | ||
case cerrors.Is(err, pipeline.ErrPipelineNotRunning): | ||
return codes.FailedPrecondition | ||
case cerrors.Is(err, pipeline.ErrNameAlreadyExists): | ||
return codes.AlreadyExists | ||
case cerrors.Is(err, connector.ErrConnectorRunning): | ||
return codes.FailedPrecondition | ||
case cerrors.Is(err, orchestrator.ErrPipelineHasConnectorsAttached): | ||
return codes.FailedPrecondition | ||
case cerrors.Is(err, orchestrator.ErrPipelineHasProcessorsAttached): | ||
return codes.FailedPrecondition | ||
case cerrors.Is(err, orchestrator.ErrConnectorHasProcessorsAttached): | ||
return codes.FailedPrecondition | ||
default: | ||
return codes.Internal | ||
} | ||
} |
Oops, something went wrong.