Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Remove unclear HTTP status code #646

Merged
merged 2 commits into from
Jul 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions clients/http/deviceprofile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ func TestAddDeviceProfileByYaml(t *testing.T) {
res, err := client.AddByYaml(context.Background(), testCase.filePath)
if testCase.errorExpected {
require.True(t, errors.Is(err, os.ErrNotExist))
assert.Equal(t, edgexErrors.KindClientError, edgexErrors.Kind(err))
assert.Equal(t, edgexErrors.ClientErrorCode, res.StatusCode)
assert.Equal(t, edgexErrors.KindIOError, edgexErrors.Kind(err))
} else {
require.NoError(t, err)
assert.Equal(t, requestId, res.RequestId)
Expand Down Expand Up @@ -158,8 +157,7 @@ func TestUpdateDeviceProfileByYaml(t *testing.T) {
res, err := client.UpdateByYaml(context.Background(), testCase.filePath)
if testCase.errorExpected {
require.True(t, errors.Is(err, os.ErrNotExist))
assert.Equal(t, edgexErrors.KindClientError, edgexErrors.Kind(err))
assert.Equal(t, edgexErrors.ClientErrorCode, res.StatusCode)
assert.Equal(t, edgexErrors.KindIOError, edgexErrors.Kind(err))
} else {
require.NoError(t, err)
assert.Equal(t, requestId, res.RequestId)
Expand Down
18 changes: 9 additions & 9 deletions clients/http/utils/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func makeRequest(req *http.Request) (*http.Response, errors.EdgeX) {
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, errors.NewCommonEdgeX(errors.KindClientError, "failed to send a http request", err)
return nil, errors.NewCommonEdgeX(errors.KindServerError, "failed to send a http request", err)
}
if resp == nil {
return nil, errors.NewCommonEdgeX(errors.KindServerError, "the response should not be a nil", nil)
Expand All @@ -68,15 +68,15 @@ func makeRequest(req *http.Request) (*http.Response, errors.EdgeX) {
func createRequest(ctx context.Context, httpMethod string, baseUrl string, requestPath string, requestParams url.Values) (*http.Request, errors.EdgeX) {
u, err := url.Parse(baseUrl)
if err != nil {
return nil, errors.NewCommonEdgeX(errors.KindClientError, "fail to parse baseUrl", err)
return nil, errors.NewCommonEdgeX(errors.KindServerError, "fail to parse baseUrl", err)
}
u.Path = requestPath
if requestParams != nil {
u.RawQuery = requestParams.Encode()
}
req, err := http.NewRequest(httpMethod, u.String(), nil)
if err != nil {
return nil, errors.NewCommonEdgeX(errors.KindClientError, "failed to create a http request", err)
return nil, errors.NewCommonEdgeX(errors.KindServerError, "failed to create a http request", err)
}
req.Header.Set(common.CorrelationHeader, correlatedId(ctx))
return req, nil
Expand All @@ -95,7 +95,7 @@ func createRequestWithRawData(ctx context.Context, httpMethod string, url string

req, err := http.NewRequest(httpMethod, url, bytes.NewReader(jsonEncodedData))
if err != nil {
return nil, errors.NewCommonEdgeX(errors.KindClientError, "failed to create a http request", err)
return nil, errors.NewCommonEdgeX(errors.KindServerError, "failed to create a http request", err)
}
req.Header.Set(common.ContentType, content)
req.Header.Set(common.CorrelationHeader, correlatedId(ctx))
Expand All @@ -110,7 +110,7 @@ func createRequestWithEncodedData(ctx context.Context, httpMethod string, url st

req, err := http.NewRequest(httpMethod, url, bytes.NewReader(data))
if err != nil {
return nil, errors.NewCommonEdgeX(errors.KindClientError, "failed to create a http request", err)
return nil, errors.NewCommonEdgeX(errors.KindServerError, "failed to create a http request", err)
}
req.Header.Set(common.ContentType, content)
req.Header.Set(common.CorrelationHeader, correlatedId(ctx))
Expand All @@ -121,24 +121,24 @@ func createRequestWithEncodedData(ctx context.Context, httpMethod string, url st
func createRequestFromFilePath(ctx context.Context, httpMethod string, url string, filePath string) (*http.Request, errors.EdgeX) {
fileContents, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, errors.NewCommonEdgeX(errors.KindClientError, fmt.Sprintf("fail to read file from %s", filePath), err)
return nil, errors.NewCommonEdgeX(errors.KindIOError, fmt.Sprintf("fail to read file from %s", filePath), err)
}

body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
formFileWriter, err := writer.CreateFormFile("file", filepath.Base(filePath))
if err != nil {
return nil, errors.NewCommonEdgeX(errors.KindClientError, "fail to create form data", err)
return nil, errors.NewCommonEdgeX(errors.KindServerError, "fail to create form data", err)
}
_, err = io.Copy(formFileWriter, bytes.NewReader(fileContents))
if err != nil {
return nil, errors.NewCommonEdgeX(errors.KindClientError, "fail to copy file to form data", err)
return nil, errors.NewCommonEdgeX(errors.KindIOError, "fail to copy file to form data", err)
}
writer.Close()

req, err := http.NewRequest(httpMethod, url, body)
if err != nil {
return nil, errors.NewCommonEdgeX(errors.KindClientError, "failed to create a http request", err)
return nil, errors.NewCommonEdgeX(errors.KindServerError, "failed to create a http request", err)
}
req.Header.Set(common.ContentType, writer.FormDataContentType())
req.Header.Set(common.CorrelationHeader, correlatedId(ctx))
Expand Down
12 changes: 2 additions & 10 deletions errors/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,11 @@ const (
KindServiceLocked ErrKind = "ServiceLocked"
KindNotImplemented ErrKind = "NotImplemented"
KindRangeNotSatisfiable ErrKind = "RangeNotSatisfiable"
KindClientError ErrKind = "ClientError"
KindIOError ErrKind = "IOError"
KindOverflowError ErrKind = "OverflowError"
KindNaNError ErrKind = "NaNError"
)

// Error codes are not defined in HTTP status codes
const (
ClientErrorCode int = 0
)

// EdgeX provides an abstraction for all internal EdgeX errors.
// This exists so that we can use this type in our method signatures and return nil which will fit better with the way
// the Go builtin errors are normally handled.
Expand Down Expand Up @@ -214,8 +208,8 @@ func codeMapping(kind ErrKind) int {
return http.StatusMethodNotAllowed
case KindRangeNotSatisfiable:
return http.StatusRequestedRangeNotSatisfiable
case KindClientError, KindIOError:
return ClientErrorCode
case KindIOError:
return http.StatusForbidden
default:
return http.StatusInternalServerError
}
Expand Down Expand Up @@ -246,8 +240,6 @@ func KindMapping(code int) ErrKind {
return KindNotAllowed
case http.StatusRequestedRangeNotSatisfiable:
return KindRangeNotSatisfiable
case ClientErrorCode:
return KindClientError
default:
return KindUnknown
}
Expand Down