buildErrorsData builds the error data for all the error responses in the endpoint expression. The response headers, cookies and body for each response are inferred from the method's error expression if not specified explicitly.
(e *expr.HTTPEndpointExpr, sd *ServiceData)
| 1869 | // endpoint expression. The response headers, cookies and body for each response |
| 1870 | // are inferred from the method's error expression if not specified explicitly. |
| 1871 | func (sds *ServicesData) buildErrorsData(e *expr.HTTPEndpointExpr, sd *ServiceData) []*ErrorGroupData { |
| 1872 | var ( |
| 1873 | svc = sd.Service |
| 1874 | ep = svc.Method(e.MethodExpr.Name) |
| 1875 | httpclictx = httpContext(sd.Scope, false, false) |
| 1876 | ) |
| 1877 | |
| 1878 | data := make(map[string][]*ErrorData) |
| 1879 | for _, v := range e.HTTPErrors { |
| 1880 | v.Response.Body = makeHTTPType(v.Response.Body) |
| 1881 | var ( |
| 1882 | init *InitData |
| 1883 | body = v.Response.Body.Type |
| 1884 | ) |
| 1885 | |
| 1886 | pkg := pkgWithDefault(ep.ErrorLocs[v.Name], svc.PkgName) |
| 1887 | errctx := serviceContext(pkg, sd.Service.Scope) |
| 1888 | |
| 1889 | if needInit(v.Type) { |
| 1890 | var ( |
| 1891 | name string |
| 1892 | desc string |
| 1893 | isObject bool |
| 1894 | args []*InitArgData |
| 1895 | ) |
| 1896 | name = fmt.Sprintf("New%s%s", codegen.Goify(ep.Name, true), codegen.Goify(v.ErrorExpr.Name, true)) |
| 1897 | desc = fmt.Sprintf("%s builds a %s service %s endpoint %s error.", |
| 1898 | name, svc.Name, e.Name(), v.ErrorExpr.Name) |
| 1899 | headers := sds.extractHeaders(v.Response.Headers, v.AttributeExpr, errctx, sd.Scope) |
| 1900 | cookies := sds.extractCookies(v.Response.Cookies, v.AttributeExpr, errctx, sd.Scope) |
| 1901 | argsCap := len(headers) + len(cookies) |
| 1902 | if body != expr.Empty { |
| 1903 | argsCap++ |
| 1904 | } |
| 1905 | args = make([]*InitArgData, 0, argsCap) |
| 1906 | if body != expr.Empty { |
| 1907 | isObject = expr.IsObject(body) |
| 1908 | ref := "body" |
| 1909 | if isObject { |
| 1910 | ref = "&body" |
| 1911 | } |
| 1912 | args = append(args, &InitArgData{ |
| 1913 | Ref: ref, |
| 1914 | AttributeData: &AttributeData{Name: "body", VarName: "body", TypeRef: sd.Scope.GoTypeRef(v.Response.Body)}, |
| 1915 | }) |
| 1916 | } |
| 1917 | for _, h := range headers { |
| 1918 | args = append(args, &InitArgData{ |
| 1919 | Ref: h.VarName, |
| 1920 | AttributeData: &AttributeData{ |
| 1921 | Name: h.Name, |
| 1922 | VarName: h.VarName, |
| 1923 | FieldName: h.FieldName, |
| 1924 | FieldPointer: false, |
| 1925 | FieldType: h.FieldType, |
| 1926 | TypeRef: h.TypeRef, |
| 1927 | Type: h.Type, |
| 1928 | Validate: h.Validate, |
no test coverage detected