Skip to content

Commit

Permalink
add utility for reading http status from psrpc error
Browse files Browse the repository at this point in the history
  • Loading branch information
paulwe committed Nov 20, 2024
1 parent 8cab0f9 commit ea637fa
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
29 changes: 29 additions & 0 deletions rpc/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2023 LiveKit, 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 rpc

import (
"errors"

"github.com/livekit/psrpc"
)

func ErrHttpStatus(err error) (int, bool) {
var psrpcErr psrpc.Error
if errors.As(err, &psrpcErr) {
return psrpcErr.ToHttp(), true
}
return 0, false
}
30 changes: 30 additions & 0 deletions rpc/error_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2023 LiveKit, 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 rpc

import (
"net/http"
"testing"

"github.com/stretchr/testify/require"

"github.com/livekit/psrpc"
)

func TestErrHttpStatus(t *testing.T) {
code, ok := ErrHttpStatus(psrpc.NewErrorf(psrpc.NotFound, "not found"))
require.True(t, ok)
require.Equal(t, http.StatusNotFound, code)
}

0 comments on commit ea637fa

Please sign in to comment.