Skip to content

Commit a79f2e2

Browse files
authored
Chore: Mark downstream errors and fix error with no package selected (#337)
1 parent e214646 commit a79f2e2

File tree

4 files changed

+54
-5
lines changed

4 files changed

+54
-5
lines changed

pkg/github/client/errorsourcehandling.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,28 @@ import (
1414
)
1515

1616
var statusErrorStringFromGraphQLPackage = "non-200 OK status code: "
17+
// Identified downstream errors. Unfortunately, could not find a better way to identify them.
18+
var (
19+
downstreamErrors = []string{
20+
"Could not resolve to",
21+
"Your token has not been granted the required scopes to execute this query",
22+
}
23+
)
1724

1825
func addErrorSourceToError(err error, resp *googlegithub.Response) error {
1926
// If there is no error then return nil
2027
if err == nil {
2128
return nil
2229
}
2330

24-
if errors.Is(err, syscall.ECONNREFUSED) {
31+
if errors.Is(err, syscall.ECONNREFUSED) || errors.Is(err, context.Canceled) {
2532
return errorsource.DownstreamError(err, false)
2633
}
2734

28-
if errors.Is(err, context.Canceled) {
29-
return errorsource.DownstreamError(err, false)
35+
for _, downstreamError := range downstreamErrors {
36+
if strings.Contains(err.Error(), downstreamError) {
37+
return errorsource.DownstreamError(err, false)
38+
}
3039
}
3140
// Unfortunately graphql library that is used is not returning original error from the client.
3241
// It creates a new error with "non-200 OK status code: ..." error message. It includes status code

pkg/github/client/errorsourcehandling_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ func TestAddErrorSourceToError(t *testing.T) {
3838
resp: nil,
3939
expected: errorsource.SourceError(backend.ErrorSourceFromHTTPStatus(404), errors.New("non-200 OK status code: 404 Not Found"), false),
4040
},
41+
{
42+
name: "identified downstream graphql error",
43+
err: errors.New("Your token has not been granted the required scopes to execute this query"),
44+
resp: nil,
45+
expected: errorsource.DownstreamError(errors.New("Your token has not been granted the required scopes to execute this query"),false),
46+
},
4147
{
4248
name: "response with non-2xx status code",
4349
err: errors.New("some other error"),
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import React from 'react';
2+
import QueryEditorIssues, { DefaultPackageType } from './QueryEditorPackages';
3+
import { render } from '@testing-library/react';
4+
import { PackageType } from 'types';
5+
6+
describe('QueryEditorPackages', () => {
7+
it('should update package type to default one if no package is selected', async () => {
8+
const props = {
9+
onChange: jest.fn(),
10+
packageType: undefined,
11+
};
12+
render(<QueryEditorIssues {...props} />);
13+
expect(props.onChange).toHaveBeenCalledTimes(1);
14+
expect(props.onChange).toHaveBeenCalledWith({ packageType: DefaultPackageType, onChange: props.onChange });
15+
});
16+
it('should not update package type to default one if package type is provided ', async () => {
17+
const props = {
18+
onChange: jest.fn(),
19+
packageType: PackageType.DOCKER,
20+
};
21+
render(<QueryEditorIssues {...props} />);
22+
expect(props.onChange).not.toHaveBeenCalled();
23+
});
24+
});

src/views/QueryEditorPackages.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState } from 'react';
1+
import React, { useEffect, useState } from 'react';
22

33
import { Input, Select, InlineField } from '@grafana/ui';
44
import { SelectableValue } from '@grafana/data';
@@ -10,7 +10,7 @@ interface Props extends PackagesOptions {
1010
onChange: (value: PackagesOptions) => void;
1111
}
1212

13-
const DefaultPackageType = PackageType.NPM;
13+
export const DefaultPackageType = PackageType.NPM;
1414

1515
const packageTypeOptions: Array<SelectableValue<string>> = Object.keys(PackageType).map((v) => {
1616
return {
@@ -22,6 +22,16 @@ const packageTypeOptions: Array<SelectableValue<string>> = Object.keys(PackageTy
2222
const QueryEditorPackages = (props: Props) => {
2323
const [names, setNames] = useState<string>(props.names || '');
2424

25+
// Set default package type if not set
26+
useEffect(() => {
27+
if (!props.packageType) {
28+
props.onChange({
29+
...props,
30+
packageType: DefaultPackageType,
31+
});
32+
}
33+
}, [props]);
34+
2535
return (
2636
<>
2737
<InlineField labelWidth={LeftColumnWidth * 2} label="Package type">

0 commit comments

Comments
 (0)