@@ -37,27 +37,27 @@ type RunPlanButtonProps = {
3737// This will be another PR
3838// See https://github.com/DiamondLightSource/mx-daq-ui/issues/71
3939
40- function readInstrumentSessionFromVisitPv ( ) : string {
40+ function readVisitFromPv ( ) : string {
4141 const fullVisitPath : RawValue = ReadPvRawValue ( {
4242 label : "visit" ,
4343 pv : "ca://BL24I-MO-IOC-13:GP100" ,
4444 } ) ;
4545 const visitString : string = forceString ( fullVisitPath ) ;
46- let instrumentSession : string | undefined = "" ;
47- if ( visitString === "not connected" || ! visitString ) {
48- const msg : string =
46+ return visitString ;
47+ }
48+
49+ function parseInstrumentSession ( visit : string ) : string {
50+ let instrumentSession : string | undefined ;
51+ if ( visit === "not connected" || visit === "undefined" ) {
52+ const msg =
4953 "Unable to run plan as instrument session not set. Please check visit PV." ;
50- console . log ( msg ) ;
51- // throw new Error(msg);
54+ throw new Error ( msg ) ;
5255 } else {
53- if ( visitString . endsWith ( "/" ) ) {
54- instrumentSession = visitString . split ( "/" ) . at ( - 2 ) ;
55- } else {
56- instrumentSession = visitString . split ( "/" ) . at ( - 1 ) ;
57- }
56+ instrumentSession = visit . split ( "/" ) . filter ( Boolean ) . at ( - 1 ) ;
5857 if ( ! instrumentSession ) {
59- console . log ( "NOPE" ) ;
60- throw new Error ( "Something is wrong with visit path" ) ;
58+ throw new Error (
59+ "Unable to run plan as something appears to be wrong with visit path"
60+ ) ;
6161 }
6262 }
6363 return instrumentSession ;
@@ -68,25 +68,35 @@ export function RunPlanButton(props: RunPlanButtonProps) {
6868 const [ msg , setMsg ] = React . useState < string > ( "Running plan..." ) ;
6969 const [ severity , setSeverity ] = React . useState < SeverityLevel > ( "info" ) ;
7070
71- const instrumentSession = readInstrumentSessionFromVisitPv ( ) ;
71+ const fullVisit = readVisitFromPv ( ) ;
72+ let instrumentSession : string ;
7273
7374 const params = props . planParams ? props . planParams : { } ;
7475 const variant = props . btnVariant ? props . btnVariant : "outlined" ;
7576 const size = props . btnSize ? props . btnSize : "medium" ;
7677
7778 const handleClick = ( ) => {
7879 setOpenSnackbar ( true ) ;
79- submitAndRunPlanImmediately ( {
80- planName : props . planName ,
81- planParams : params ,
82- instrumentSession : instrumentSession ,
83- } ) . catch ( ( error ) => {
80+ try {
81+ instrumentSession = parseInstrumentSession ( fullVisit ) ;
82+ submitAndRunPlanImmediately ( {
83+ planName : props . planName ,
84+ planParams : params ,
85+ instrumentSession : instrumentSession ,
86+ } ) . catch ( ( error ) => {
87+ setSeverity ( "error" ) ;
88+ setMsg (
89+ `Failed to run plan ${ props . planName } , see console and logs for full error`
90+ ) ;
91+ console . log ( `${ msg } . Reason: ${ error } ` ) ;
92+ } ) ;
93+ } catch ( error ) {
8494 setSeverity ( "error" ) ;
8595 setMsg (
86- `Failed to run plan ${ props . planName } , see console and logs for full error `
96+ `Failed to run plan ${ props . planName } , please check visit PV is set. `
8797 ) ;
88- console . log ( `${ msg } . Reason: ${ error } ` ) ;
89- } ) ;
98+ console . log ( `An error occurred ${ error } ` ) ;
99+ }
90100 } ;
91101
92102 const handleSnackbarClose = (
0 commit comments