@@ -5,7 +5,6 @@ import { z } from 'zod';
55import {
66 codeSelector ,
77 clippyRequestSelector ,
8- formatRequestSelector ,
98 getCrateType ,
109 runAsTest ,
1110 useWebsocketSelector ,
@@ -107,9 +106,6 @@ export enum ActionType {
107106 EnableFeatureGate = 'ENABLE_FEATURE_GATE' ,
108107 GotoPosition = 'GOTO_POSITION' ,
109108 SelectText = 'SELECT_TEXT' ,
110- RequestFormat = 'REQUEST_FORMAT' ,
111- FormatSucceeded = 'FORMAT_SUCCEEDED' ,
112- FormatFailed = 'FORMAT_FAILED' ,
113109 RequestClippy = 'REQUEST_CLIPPY' ,
114110 ClippySucceeded = 'CLIPPY_SUCCEEDED' ,
115111 ClippyFailed = 'CLIPPY_FAILED' ,
@@ -287,6 +283,21 @@ async function fetchJson(url: FetchArg, args: RequestInit) {
287283 }
288284}
289285
286+ // We made some strange decisions with how the `fetchJson` function
287+ // communicates errors, so we untwist those here to fit better with
288+ // redux-toolkit's ideas.
289+ export const adaptFetchError = async < R > ( cb : ( ) => Promise < R > ) : Promise < R > => {
290+ try {
291+ return await cb ( ) ;
292+ } catch ( e ) {
293+ if ( e && typeof e === 'object' && 'error' in e && typeof e . error === 'string' ) {
294+ throw new Error ( e . error ) ;
295+ } else {
296+ throw new Error ( 'An unknown error occurred' ) ;
297+ }
298+ }
299+ }
300+
290301interface ExecuteRequestBody {
291302 channel : string ;
292303 mode : string ;
@@ -567,46 +578,6 @@ export const gotoPosition = (line: string | number, column: string | number) =>
567578export const selectText = ( start : Position , end : Position ) =>
568579 createAction ( ActionType . SelectText , { start, end } ) ;
569580
570- const requestFormat = ( ) =>
571- createAction ( ActionType . RequestFormat ) ;
572-
573- interface FormatRequestBody {
574- code : string ;
575- edition : string ;
576- }
577-
578- interface FormatResponseBody {
579- success : boolean ;
580- code : string ;
581- stdout : string ;
582- stderr : string ;
583- }
584-
585- const receiveFormatSuccess = ( body : FormatResponseBody ) =>
586- createAction ( ActionType . FormatSucceeded , body ) ;
587-
588- const receiveFormatFailure = ( body : FormatResponseBody ) =>
589- createAction ( ActionType . FormatFailed , body ) ;
590-
591- export function performFormat ( ) : ThunkAction {
592- // TODO: Check a cache
593- return function ( dispatch , getState ) {
594- dispatch ( requestFormat ( ) ) ;
595-
596- const body : FormatRequestBody = formatRequestSelector ( getState ( ) ) ;
597-
598- return jsonPost < FormatResponseBody > ( routes . format , body )
599- . then ( json => {
600- if ( json . success ) {
601- dispatch ( receiveFormatSuccess ( json ) ) ;
602- } else {
603- dispatch ( receiveFormatFailure ( json ) ) ;
604- }
605- } )
606- . catch ( json => dispatch ( receiveFormatFailure ( json ) ) ) ;
607- } ;
608- }
609-
610581interface GeneralSuccess {
611582 stdout : string ;
612583 stderr : string ;
@@ -924,9 +895,6 @@ export type Action =
924895 | ReturnType < typeof enableFeatureGate >
925896 | ReturnType < typeof gotoPosition >
926897 | ReturnType < typeof selectText >
927- | ReturnType < typeof requestFormat >
928- | ReturnType < typeof receiveFormatSuccess >
929- | ReturnType < typeof receiveFormatFailure >
930898 | ReturnType < typeof requestClippy >
931899 | ReturnType < typeof receiveClippySuccess >
932900 | ReturnType < typeof receiveClippyFailure >
0 commit comments